简体   繁体   English

在Java中,存储大列表最有效的内存方式是什么

[英]In Java, what is the most memory efficient way to store large lists

I have to to compare two large arrays of Integers(approximatly 150,000 values) to see which values are unique and which are not. 我必须比较两个大型的整数数组(大约150,000个值),以查看哪些值是唯一的,哪些不是。 I want the output to be stored in three data structures, UniqueTo_A, UniqueTo_B & SharedElements. 我希望将输出存储在三个数据结构中,UniqueTo_A,UniqueTo_B和SharedElements。 I would ordinary use something like an ArrayList for this sort of operation, as Values can be added ad-hoc, however I am aware the overheads for add() and remove() are rather large for ArrayLists. 我通常会使用类似ArrayList之类的方法进行此类操作,因为可以临时添加值,但是我知道add()和remove()的开销对于ArrayLists而言相当大。 So my question is:- In Java, what is the most memory efficient way to store large lists, that items can be added dynamically, performance is the key. 所以我的问题是:-在Java中,存储大型列表最有效的内存方式是什么,可以动态添加项目,性能是关键。 All help or comments will be much appreciated. 所有帮助或评论将不胜感激。

Edit: Thanks for you're input's people. 编辑:感谢您输入的人。 TheLostMind, I will need to add to datasets but the Hashset will facilitate this, so I'm going to go ahead and use a Hashset. TheLostMind,我将需要添加到数据集,但是Hashset将帮助实现这一点,因此,我将继续使用Hashset。 Nafas + NeplatnyUdaj thanks for examples. Nafas + NeplatnyUdaj感谢您的示例。 Eckles, I should get to grips with collections I'll study this to use another time. 烦躁不安的是,我应该处理一下集合,我将继续研究这个集合。 Implementation to follow.... 后续执行...。

I don't think lists are a very good way to do that. 我认为列表不是执行此操作的好方法。 Do you need to preserve the order of elements? 您需要保留元素的顺序吗? Can a single list contain duplicate entries? 一个列表可以包含重复的条目吗? If not, then I would go with HashSets like this: 如果没有,那么我将使用HashSets这样:

    //initialization
    Random r = new Random();
    Set<Integer> aSet = new HashSet<Integer>();
    Set<Integer> bSet = new HashSet<Integer>();
    for (int i = 0; i< 150000; i++){
        aSet.add(r.nextInt());
        bSet.add(r.nextInt());
    }

    //Computation
    Set<Integer> aUnique = new HashSet<Integer>();
    Set<Integer> bUnique = new HashSet<Integer>(bSet); //we will remove duplicate entries later
    Set<Integer> shared = new HashSet<Integer>();
    for (Integer aval: aSet){
        if (bSet.contains(aval)){
            shared.add(aval);
        }else{
            aUnique.add(aval);
        }
    }
    bUnique.removeAll(shared);

In the end, you get three sets as requested( aUnique , bUnique and shared ) 最后,您将按要求获得三组( aUniquebUniqueshared

use Set , because, it adds in constant time, it removes multiple values in constant time . 使用Set ,因为它在恒定时间内adds在恒定时间内removes多个值。 I use set in daily basis with over millions of stored Objects. 我每天使用set来存储超过数百万个对象。 and removeAll still in miliseconds removeAll仍然以毫秒为单位

Set<Integer> setA= new HashSet<Integer>();
Set<Integer> setB= new HashSet<Integer>();

//add stuff to setA and setB by add() method

Set<Integer> uniqueToA=new HashSet<Integer>(setA);
Set<Integer> uniqueToB=new HashSet<Integer>(setB);
Set<Integer> shared=new HashSet<Integer>();
shared.addAll(setA);
shared.addAll(setB);

uniqueToA.removeAll(setB);
uniqueToB.removeAll(setA);

shared.removeAll(uniqueToA);
shared.removeAll(uniqueToB);

System.out.println(uniqueToA);  //unique to A
System.out.println(uniqueToB); //unique To B
System.out.println(shared);  //shared

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

相关问题 Java:读取相对较大的txt文件并存储其数据的最有效方法是什么? - Java: What's the most efficient way to read relatively large txt files and store its data? 用Java编写大型文本文件的最有效方法是什么? - What's the most efficient way to write large text file in java? 在Java中将地图存储到哪个内存最有效? - Which is most memory efficient to store map in java? 存储分析信标的最有效方法是什么? - What is the most efficient way to store analytics beacons? 在Java中增长数组的大多数内存有效方法? - Most memory efficient way to grow an array in Java? Java:在内存中存储大数据(&gt; 2GB)的官方方法是什么? - Java: what is the official way to store large data (>2GB) in memory? 存储和搜索大量字符串的最有效方法 - Most efficient way to store and search through a large number of strings 在java中模拟倒计时的最有效方法是什么? - What is the most efficient way to simulate a countdown in java? Java — 同步 ArrayList 的最有效方法是什么? - Java — What is the most efficient way to synchronize an ArrayList? 在java中连接两个大(超过1.5GB)文件的最有效(最快)方法是什么? - What is the most efficient (fastest) way to concatenate two large (over 1.5GB) files in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM