Java 如何动态创建HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19131213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:22:37  来源:igfitidea点击:

How to create HashMap dynamically

java

提问by Hanumath

I have .txtfile with 5L values(Lines) in the following way and also have partition size 50000.

我有.txt以下方式的 5L 值(行)的文件,也有分区大小50000

 1
 3
-1546.9
-67.90
3456
.
.
.

With the following example you can understand what is mean by Partition in my point of view.

通过以下示例,您可以理解我认为 Partition 的含义。

after importing file data into list we can see in the following way.

将文件数据导入列表后,我们可以看到以下方式。

 importingdata={1,2,3,4,5,.........500000};

After partition, you can see in the following way.

分区后,可以看到如下方式。

PartitionList={{1,2,3,...50000},{50001,....,100000},{100001,......,150000},...{450000,.....500000}};

partitionList datatype is ArrayList<HashMap<Integer,Double>>.it means all sub-lists of partitionlist was HashMap<Integer,Double>.

partitionList 数据类型是 . 这ArrayList<HashMap<Integer,Double>>意味着partitionlist 的所有子列表都是HashMap<Integer,Double>

All HashMaplists starts their key values from 1 to 50000.like in the following way.

所有HashMap列表都以1 to 50000如下方式从.like开始它们的键值。

     PartitionList={{1->1,2->2,3->3,...,50000->50000},{1->50001,.....,50000->100000},{1->100001,....,50000->150000},...,{1->450000,.....,50000->500000}};

I want to arrange file data in the above way while file importing time.

我想在文件导入时按上述方式排列文件数据。

for this I tried with sample code but it's not working.

为此,我尝试使用示例代码,但它不起作用。

public static void main(String[] args) {
    ArrayList<HashMap<Integer, Double>> finalList=new ArrayList<>();
    HashMap<Integer, Double> hash1=new HashMap<>();
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    hash1.clear();
    System.out.println(hash1.size());
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    hash1.clear();
    System.out.println(hash1.size());
    HashMap<Integer, Double> hash2=finalList.get(1);
    HashMap<Integer, Double> hash3=finalList.get(2);
    System.out.println(hash2.size());
    System.out.println(hash3.size());
}

I hope,you guys understand what I am trying. Here I mention 5Llines, but in my real case I am dealing with 80LSo suggest me with optimized code.

我希望,你们明白我在尝试什么。在这里我提到了5L行,但在我的真实案例中我正在处理80L所以建议我使用优化的代码。

Thanks

谢谢

采纳答案by ppeterka

HashMap is mutable!The hashMap references are still the same. When you do this:

HashMap 是可变的!hashMap 引用仍然相同。当你这样做时:

hash1.clear();

You clear the originalmap instance. This means, the map instance you put into the list gets cleared.

您清除原始地图实例。这意味着,您放入列表中的地图实例将被清除。

You should do

你应该做

hash1 = new HashMap<Integer, Double>();

instead. This updates the variable's reference to a new instanceof HashMap().

反而。这会更新变量对 HashMap()新实例的引用。

回答by Autocrab

public static void main(String[] args) {
    List<Map<Integer, Double>> finalList=new ArrayList<Map<Integer, Double>>();
    Map<Integer, Double> hash1=new HashMap<Integer, Double>();
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    hash1 = new HashMap<Integer, Double>();
    System.out.println(hash1.size());
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    // and so on
}

回答by Pratik Shelar

There are a few issues with your sample code.

您的示例代码存在一些问题。

  1. You need to create new instances of the hashmap once you have added the 50000 entries into it. Calling clear shall not help your case.
  2. Since you are well aware of the size of the HashMap while creating the hashmap make sure to pass the initial size. Providing the initial size prevents frequent rehashing which affects the performance
  1. 添加 50000 个条目后,您需要创建哈希图的新实例。打电话清除对你的情况没有帮助。
  2. 由于您在创建 hashmap 时很清楚 HashMap 的大小,因此请确保传递初始大小。提供初始大小可以防止影响性能的频繁重新散列

HashMap<Integer, Double> hash1=new HashMap<Integer,Double>(50000);

HashMap<Integer, Double> hash1=new HashMap<Integer,Double>(50000);

  1. I am not aware if its a typo or not. In the sample code you you are trying to call get(1) whereas list indices start from 0 and not from 1.
  1. 我不知道它是否是一个错字。在示例代码中,您尝试调用 get(1) 而列表索引从 0 开始而不是从 1 开始。

For your actual problem you should have a look at Google Collections2 API. Please find below a code which will help you achieve your final goal

对于您的实际问题,您应该查看 Google Collections2 API。请在下面找到有助于您实现最终目标的代码

// Read all 80Lac lines
    List<String> allLines = Files.readAllLines(
            new File("d:/input.txt").toPath(), Charset.defaultCharset());       

    // Partition the 80L records into lists of 50000
    List<List<String>> partitionedLists = Lists.partition(allLines, 50000);

    ArrayList<ListMultimap<Integer, String>> finalList = new ArrayList<ListMultimap<Integer,String>>();
    for(final List<String> item: partitionedLists){         
        ListMultimap<Integer, String> finalMap = Multimaps.index(item.iterator(), new Function<String,Integer>(){               
            @Override
            public Integer apply(String arg0) {
                return item.indexOf(arg0);                                  
            }

        });

        finalList.add(finalMap);
    }

Even I am new to the Collections API but I tested the above code it does create a list of maps with key as the index. Only issue is that MultiMaps are normally used to achieve GROUP BY kind of operation and hence duplicate values are getting grouped together. I am working on it but meanwhile you can start your implementation using the above code.

即使我是 Collections API 的新手,但我测试了上面的代码,它确实创建了一个以键为索引的映射列表。唯一的问题是 MultiMaps 通常用于实现 GROUP BY 类型的操作,因此重复的值被组合在一起。我正在研究它,但同时你可以使用上面的代码开始你的实现。