【问题标题】:Parse Data From text File to Hashmap将文本文件中的数据解析为 Hashmap
【发布时间】:2015-06-28 11:33:44
【问题描述】:

我有一个类似图像中的文本文件,我想将这些数据放在哈希图中,看起来像这样-

Madhya Pradesh  Bhopal
        Gwalior
        Indore
        Khandwa

Goa     Mapusa
        Margao
        Bicholim
        Panaji

到目前为止,我已经能够隔离单个字符串的键和值,但我不知道从那里去哪里。接下来我需要做什么?

while ((line=br.readLine())!=null){
    state=line.split("-")[0];
    city=line.split("-")[1];
}

【问题讨论】:

  • 可以放在HashMap中吗?
  • 是的,这就是我想要做的......但是钥匙分散在各处......我怎样才能把它们放在一起?我是 hashmap 的新手,如果我不明白,请道歉

标签: java list file arraylist hashmap


【解决方案1】:

我在另一个问题中看到您正在使用 Java 8。如果是这种情况,有一种很好的方法可以完成您的任务:

import static java.nio.file.Files.lines;
import static java.nio.file.Paths.get;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;

...

Map<String, List<String>> map = lines(get("file"))
              .map(s -> s.split("-"))
              .collect(groupingBy(arr -> arr[0], mapping(arr -> arr[1], toList())));

稍微解释一下发生了什么:

  • Files.lines 创建一个 Stream&lt;String&gt; (每个字符串是文件中的一行)
  • 然后你用破折号分割每一行(就像你在 while 循环中所做的那样),这会给你一个Stream&lt;String[]&gt;
  • 然后您通过将数组与其第一个元素进行分组来收集 Stream 的内容 (arr -&gt; arr[0])。这会给你一个Map&lt;String, List&lt;String[]&gt; 1
  • 但您希望映射的值是每个数组的第二个元素,因此您使用mapping 收集器。然后将每个值收集到List 中,从而生成Map&lt;String, List&lt;String&gt;&gt;

这是 Java 8 中的惯用语,一开始可能有点难以理解,但一旦掌握了这个概念,它就会非常强大。

1.在后台这个Map&lt;String, List&lt;String[]&gt; 从未被创建,但它更容易像那样可视化

【讨论】:

    【解决方案2】:

    由于每个键都链接到多个值而不仅仅是一个值,因此您应该使用 MultiMap 接口及其来自 Apache 集合的 MultiValueMap(参见 here

    例子:

    MultiMap<String, String> myMap = new MultiValueMap<String, String>
    
    myMap.put(state, city);
    

    它将新值添加到与该键关联的集合中

    [编辑] 如果您想在不使用外部库的情况下执行此操作,您可以通过以下方式进行操作

    Map<String, List<String>> myMap = new HashMap<String, List<String>)();
    if myMap.containsKey(state) {
        List<String> cityList = myMap.get(state);
        cityList.add(city);
    } else {
        List<String> stateCityList = new ArrayList<String>();
        stateCityList.add(city);
        myMap.put(state, stateCityList);
    }
    

    这会按照你想要的方式工作。

    【讨论】:

    • 有什么方法可以在不使用外部集合和 Java 的默认集合类的情况下做到这一点?
    【解决方案3】:

    您已经将它们拆分,只需将它们添加到 MultiMap:MultiMap 的实现可以在 Apache Commons Collections 中找到。

    MultiMap 在内部创建一个集合,其中包含特定键的所有条目。然后,您可以根据需要获取这些集合。

    MultiMap stateCityMap= new MultiHashMap();
    String state;
    String city;
    while ((line=br.readLine())!=null){
        state=line.split("-")[0];
        city=line.split("-")[1];
        stateCityMap.put(state, city);
    }
    

    编辑:

    您也可以这样做,无需任何专有库:

    Map<String, List<String>> stateCityMap= new HashMap<>();
    String state;
    String city;
    while ((line=br.readLine())!=null){
        state=line.split("-")[0];
        city=line.split("-")[1];
        if(stateCityMap.get(state) == null) {
            stateCityMap.put(state, new ArrayList<String>());
        }
        stateCityMap.get(state).add(city);
    }
    

    【讨论】:

    • 有什么方法可以在不使用外部集合和 Java 的默认集合类的情况下做到这一点?
    【解决方案4】:

    你可以把它放到 List 的 Map 中。

    Map<String, List<String>> map = new HashMap<String, List<String>>();
    List<String> cityList;
    while ((line = br.readLine()) != null) {
        state = line.split("-")[0];
        city = line.split("-")[1];
    
        cityList = map.get(state);
        if (cityList == null) {
            cityList = new ArrayList<String>();
            map.put(state, cityList);
        }
        cityList.add(city);
    }
    

    【讨论】:

    • 是的,确切地说,我正在尝试创建城市列表或值的列表.. 但要做到这一点,我需要知道哪个值与哪个键相关联.. 从文件 i不能隔离。我一次只能得到一个值
    【解决方案5】:
    1. 首先,拆分的次数不要与您拥有的项目一样多:使用结果设置变量。
    2. 将所有内容放在Map&lt; String, Collection&lt;String&gt;&gt; 中(HashMap 或 TreeMap,它将按州对城市进行排序......)。第一项将对应于键,第二项将添加到相应的集合值中。

    这是一个可以说明它的代码示例:

    Map<String, List<String> > map = new TreeMap<String, Collection<String>>();
    Collection<String> cities;
    while ((line = br.readLine()) != null) {
        String[] result = line.split("-");
        state = result[0];
        city = result[1];
    
        cities= map.get(state);
        if (cities== null) {
            cities = new ArrayList<String>();
            map.put(state, cities);
        }
        cities.add(city);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多