我有一个文本文件,该文件具有打印机,然后是与该打印机关联的打印作业:
LaserJet This assignment involves . . .
LaserJet Budget for November . . .
Lexmark Invoice Number 334222: . . .
Samsung Class schedule for 2013/01/06 . . .
Lexmark Production schedule . . .
Samsung Class schedule for 2013/02/06 . . .
LaserJet Memo to Bobby Hill . . .
Lexmark Meeting in 20 minutes . . .
Lexmark Conference call transcript . . .
Lexmark Production schedule is off time . . .
Lexmark Last Production schedule . . .
LaserJet Print a crapload of pages . . .
我想将文本文件读入程序,并创建一个以打印机名称为键的HashMap,并将printjob与相应的打印机关联。所以我最终会得到类似:
LaserJet
This assignment involves . . .
udget for November . . .
Memo to Bobby Hill . . .
Print a crapload of pages . . .
Samsung
Class schedule for 2013/01/06 . . .
Class schedule for 2013/02/06 . . .
Lexmark
Invoice Number 334222: . . .
等等(该文件可能包含1000份使用许多不同打印机的作业)。我正在努力将printjob与它的打印机相关联。到目前为止,这是我的代码:
import java.util.*;
public class Printers {
private String sPrinterName;
private String sContent;
private Map<String, ArrayList<String>> mapOfJobs = new HashMap<String, ArrayList<String>>();
private ArrayList<String> collectionContent = new ArrayList<String>();
private ArrayList<String> collectionPrinters = new ArrayList<String>();
public Printers() { }
public void loadCollections( Scanner input ) {
while ( input.hasNext() ) {
sPrinterName = input.next(); // get printer name
sContent = input.nextLine(); // get string ( print job )
if ( !collectionPrinters.contains(sPrinterName))
collectionPrinters.add(sPrinterName);
collectionContent.add(this.sContent);
}
}
public void buildMap() { // the process of creating the HashMap
for ( int i = 0; i < collectionPrinters.size(); i++ ) {
String refToPrinter = collectionPrinters.get(i);
String refToContent = collectionContent.get(i);
if ( !mapOfJobs.containsKey( refToPrinter ) ) {
ArrayList<String> tmp = new ArrayList<String>();
tmp.add( refToContent );
mapOfJobs.put( refToPrinter, tmp );
}
else if ( !mapOfJobs.get( refToPrinter ).contains( refToContent ) )
mapOfJobs.get( refToPrinter ).add( refToContent );
}
}
public void displayAll() { // print everything
Iterator iterator = mapOfJobs.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = mapOfJobs.get(key).toString();
System.out.println(key + " " + value);
}
}
public String toString() {
return String.format("Printer: %s Job: %s", sPrinterName, sContent );
}
}
我有一个主要的类,它创建一个Printers对象来调用方法。无论如何,代码仅输出:
LaserJet [ This assignment involves . . .]
Lexmark [ Budget for November . . .]
Samsung [ Invoice Number 334222: . . .]
帮助表示赞赏。
您不需要那么多Collections
,仅mapOfJobs
HashMap就足够了,
public void loadResult( Scanner input ) {
while ( input.hasNextLine() ) {
String line=input.nextLine();
sPrinterName = line.subString(0, line.indexOf(' '));// printer name
sContent = line.subString(line.indexOf(' ')+1); // get string ( print job )
List<String> contentList=mapOfJobs.get(sPrinterName);
if(contentList==null ){
contentList=new ArrayList<>();
}
contentList.add(sContent);
mapOfJobs.put(sPrinterName,contentList)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句