文件列表中的HashMap [英] HashMap from List of Files

查看:122
本文介绍了文件列表中的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想探索使用HashMap来跟踪文件间变化的选项。我使用一些配置/文本文件来提供一组状态文件:

I'd like to explore the option of using a HashMap to keep track of changes between files. I'm using a few config/text files to give a set of documents of status:

配置文件如下所示:

STATUS1 = "Doc1.pdf, Doc2.xls, Doc5.doc"
STATUS2 = "Doc8.pdf, Doc6.doc"
STATUS3 = "Doc10.pdf"
...

不必创建一个单独的HashMap for每个实例如下所示:

Instead of having to create a separate HashMap for each instance like so:

Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Map<String, String> map3 = new HashMap<String, String>();

map1.put("STATUS1", "Doc1.pdf");
map2.put("STATUS1", "Doc2.xls");
map3.put("STATUS1", "Doc5.doc");

我想只有一个包含状态键和映射值的Map

I'd like to have only a single Map with the key of the status and the values mapped to that key.

在解析文件时我不需要帮助,我只需要帮助实现HashMap或Map,以便我可以添加此功能。如果还有其他数据类型或组织这些数据的方法,我想听听你的意见。

I don't need help in parsing the file, I just need assistance in implementing the HashMap or Map so I can add this functionality. If there are other datatypes or methods of organizing this data, I'd like to hear your opinions on that.

任何帮助都将非常感谢。

Any help would be much appreciated.

推荐答案

您可以使用 MultiMap ,它存储同一个键的多个值。

You can use a MultiMap, which stores multiple values for the same key.


  1. Multimap

  1. Multimap




   Multimap<String, String> myMultimap = ArrayListMultimap.create();
   // Adding some key/value

  myMultimap.put("STATUS1", "somePDF");
  myMultimap.put("STATUS1", "someDOC");
  myMultimap.put("STATUS1", "someXCL");   
  myMultimap.put("STATUS2","someFormat");

  // Getting the size
  int size = myMultimap.size();
  System.out.println(size);  // 4

  // Getting values
  Collection<string> stats1 = myMultimap.get("STATUS1");
  System.out.println(stats1); // [somePDF, someDOC, someXCL]


2。 HashMap

With HashMap you can have something like,

    List<String> listOfDocs = new ArrayList<String>();
    listOfDocs.add("somePDF");
    listOfDocs.add("someDOC");
    listOfDocs.add("someFormat");

    HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); 
    // key would be your STATUS
    // Values would be ListOfDocs you need.

   map.put("STATUS1", listOfDocs);
   map.put("STATUS2", listOfDocs2);
   map.put("STATUS3", listOfDocs3);

希望这会有所帮助。
如果您有任何疑问,请告知我。

Hope this helps. Let me know if you have questions.

这篇关于文件列表中的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆