`ArrayList of HashMap`或`LinkedHashMap`按索引获取项目 [英] `ArrayList of HashMap` or `LinkedHashMap` to get item by index

查看:954
本文介绍了`ArrayList of HashMap`或`LinkedHashMap`按索引获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在键值表单中存储大量的数据。
此外,我有两个要求


  1. 通过索引查询数据,比如从数组。

  2. 因此,必须保留数据结构中的顺序。

对于要求2 - 我可以使用 LinkedHashMap



对于需求1 - p>


  • 1.1 |实现 HashMap的ArrayList 。 [ ArrayList< HashMap< String,String>> ]

  • 要实现 LinkedHashMap ,并使用类似

    • - > 的新索引来查询项目new ArrayList(hashMapObject.entrySet ).get(0);




问题是在 1.1 1.2 中更好吗?



更好的意思是 - 在内存和空间方面的效率。



让我们假设数据量为50到100个键值对的平均大小字符串 - 例如每个键为10-30个字符,值为30-50个字符。

解决方案

我自己去做实验。原来,创建 HashMap的ArrayList 的方法对于1000个元素的速度是快40倍。

  public class HashMapVsArrayOfHashMap {

public static void main(String [] args){
ArrayList< HashMap< String,String> listOfMaps = new ArrayList< HashMap< String,String>>();
for(int i = 0; i <1000; i ++){
final int finalI = i;
listOfMaps.add(new HashMap< String,String>(){{put(asdfasdfasdfasdfadsf+ finalI,asdfsdafasdfsadfasdf+ finalI);}});
}
LinkedHashMap< String,String> map = new LinkedHashMap< String,String>();
for(int i = 0; i <1000; i ++)
map.put(asdfasdfasdfasdfadsf+ i,asdfsdafasdfsadfasdf+ i);
int position = 700;
testArrayList(Method1:ArrayListOfHashMaps,position,listOfMaps);
testHashMap(Method2:LinkedHashMap,position,map);
}

private static void testArrayList(String string,int position,
ArrayList< HashMap< String,String> listOfMaps){
long start,end;
start = System.nanoTime();
listOfMaps.get(position).get(asdfasdfasdfasdfadsf+ position);
end = System.nanoTime();
System.out.println(string +| Difference =+(end-start));
}
private static void testHashMap(String string,int position,
LinkedHashMap< String,String> map){
long start,end;
start = System.nanoTime();

String s = new ArrayList< String>(map.keySet())。get(position);

end = System.nanoTime();
System.out.println(string +| Difference =+(end-start));
}
}







当您将大小增加到30,000个元素时,差异是巨大的。 b
$ b

>






My need to store a a huge amount of data in the key-value form. Also, i have two requirements

  1. query data via the index, like from an array.
  2. hence, the order in the data structure must be preserved.

For Requirement 2 - I can use a LinkedHashMap.

For Requirement 1 - I have two options :

  • 1.1 | To implement an ArrayList Of HashMap. [ArrayList<HashMap<String,String>>]
  • 1.2 | To implement a LinkedHashMap and query the items by index using something like
    • -> new ArrayList(hashMapObject.entrySet()).get(0);

The Question is which is better among 1.1 or 1.2 ?

By better, i mean - efficient in terms of memory and space.

Let's assume the volume of data is in the order of 50 to 100 key-value pairs with average sized Strings - say every key is 10-30 characters and value is 30-50 characters.

解决方案

I went with experimentating it myself. Turns out the method of creating an ArrayList of HashMaps is about 40 times faster with 1000 elements.

public class HashMapVsArrayOfHashMap {

    public static void main(String[] args){
        ArrayList<HashMap<String, String>> listOfMaps=new ArrayList<HashMap<String,String>>();
        for( int i=0;i<1000;i++){
            final int finalI=i;
        listOfMaps.add(new HashMap<String, String>(){{put("asdfasdfasdfasdfadsf"+finalI,"asdfsdafasdfsadfasdf"+finalI);}});
        }
        LinkedHashMap<String, String> map=new LinkedHashMap<String, String>();
        for(int i=0;i<1000;i++)
            map.put("asdfasdfasdfasdfadsf"+i,"asdfsdafasdfsadfasdf"+i);     
        int position=700;
        testArrayList("Method1:ArrayListOfHashMaps",position,listOfMaps);
        testHashMap("Method2:LinkedHashMap",position,map);
    }

    private static void testArrayList(String string, int position,
            ArrayList<HashMap<String, String>> listOfMaps) {
        long start, end;
        start=System.nanoTime();
        listOfMaps.get(position).get("asdfasdfasdfasdfadsf"+position);
        end=System.nanoTime();
        System.out.println(string+"|Difference = "+(end-start));        
    }
    private static void testHashMap(String string, int position,
            LinkedHashMap<String, String> map) {
        long start, end;
        start=System.nanoTime();

        String s= new ArrayList<String>(map.keySet()).get(position);

        end=System.nanoTime();
        System.out.println(string+"|Difference = "+(end-start));        
    }
}

When you increase the size to 30,000 elements - the difference is HUGE.

这篇关于`ArrayList of HashMap`或`LinkedHashMap`按索引获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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