将值与文本文件中的HashMap中的键相关联[Java]

时间:2013-12-09 05:04:41

标签: java hashmap

我有一个文本文件,其中包含一台打印机,后跟与该打印机关联的打印作业:

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 );
}
}

我有另一个使用main的类,它创建一个Printers对象来调用方法。无论如何,代码只输出:

LaserJet [ This assignment involves . . .]
Lexmark [ Budget for November . . .]
Samsung [ Invoice Number 334222: . . .]

帮助表示赞赏..

3 个答案:

答案 0 :(得分:1)

如果我理解正确,您的问题是您想要从文件中读取作业并将其发送到打印机进行打印。

到目前为止,您已完成地图创建,并且您将面临打印作业的问题。

如果我错过了任何一点,请告诉我。

我认为在填充HashMap时你可以使用Map&gt;这样您就可以为每台打印机维护一个队列。如果您希望打印机的内容是唯一的,即同一内容无法打印两次,您可以使用地图&gt;。

不确定它是否对您有所帮助。如果您有任何具体问题,请与我们联系。

答案 1 :(得分:1)

您不需要这么多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)
  }
}

答案 2 :(得分:1)

在致电loadCollections后,收藏集会有不同的行数。如果示例文本是完整文件,则collectionPrinters中将包含3行,collectionContent中将包含12行。并且您已删除了打印机和内容之间的关联。

您只需要使用伪代码构建地图:

for each line
  if no map entry
    create map entry with content
  else
    append content to map entry