简体   繁体   English

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

[英]Associating values with keys in a HashMap from text file [Java]

I have a text file that has a printer followed by a print job associated with that printer: 我有一个文本文件,其中包含一个打印机,后跟与该打印机关联的打印作业:

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 . . .

I want to read the text file into my program and create a HashMap with the printer name as the key and associate the printjob with its corresponding printer. 我想将文本文件读入我的程序并创建一个以打印机名称为键的HashMap,并将printjob与其相应的打印机相关联。 so i will end up with something like: 所以我最终会得到类似的东西:

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: . . .

etc. ( the file could contain 1000's of jobs with many different printers ). 等(该文件可能包含1000个具有许多不同打印机的作业)。 I am struggling with associating a printjob with its printer though. 我正在努力将printjob与其打印机相关联。 this is my code so far: 到目前为止这是我的代码:

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

i have another class with main that creates a Printers object to call the methods. 我有另一个使用main的类,它创建一个Printers对象来调用方法。 Anyway the code only outputs: 无论如何代码只输出:

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

help is appreciated.. 帮助表示赞赏..

If I have understood properly, your issue is you want to read the jobs from a file and send that to a printer for printings. 如果我已正确理解,您的问题是您想要从文件中读取作业并将其发送到打印机进行打印。

Till now you are done with the Map creation and you are facing issue with the print job. 到目前为止,您已完成地图创建,并且您将面临打印作业的问题。

Please let me know if I have missed any point here. 如果我错过了任何一点,请告诉我。

I think while populating the HashMap you can use Map> so that you can maintain a queue for each printer. 我认为在填充HashMap时,您可以使用Map>,这样您就可以为每台打印机维护一个队列。 If you want the content of a printer to be unique ie same content can not be printed twice you can use Map>. 如果您希望打印机的内容是唯一的,即相同的内容无法打印两次,您可以使用Map>。

Not sure if it helps you. 不确定它是否对你有所帮助。 Let me know if you have any specific questions. 如果您有任何具体问题,请与我们联系。

You don't need so many Collections , only mapOfJobs HashMap is enough, 你不需要那么多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)
  }
}

After your call to loadCollections the collections will have different number of rows. 在调用loadCollections ,集合将具有不同的行数。 If the example text was the complete file you would have 3 rows in collectionPrinters and 12 rows in collectionContent. 如果示例文本是完整文件,则collectionPrinters中将包含3行,collectionContent中将包含12行。 And you have removed the association between printers and content. 并且您已删除了打印机和内容之间的关联。

You just need to build your map, in pseudo code: 您只需要使用伪代码构建地图:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM