Java 如何使用ArrayList、LinkedList或TreeMap?

Java 如何使用ArrayList、LinkedList或TreeMap?,java,performance,data-structures,Java,Performance,Data Structures,在我的问题中,我有一大组数字。在运行时,我想在这个已排序的数字序列中插入一个数字,并获取插入的数字的下一个(升序下一个)(所有内容都已排序),然后删除插入的数字 还有一个限制条件,有时我想插入的数字正好等于初始集合中的其他数字,在这种情况下,我也想获取下一个数字 我不知道是应该使用树状图(因为树状图总是被排序的,但有一个键的开销)、LinkedList(因为输入和删除值变得更容易)还是简单的 ArrayList 我将这些数字载入内存并进行密集计算,我的平台是移动的,所以我想要一些高效的东西。有什

在我的问题中,我有一大组数字。在运行时,我想在这个已排序的数字序列中插入一个数字,并获取插入的数字的下一个(升序下一个)(所有内容都已排序),然后删除插入的数字

还有一个限制条件,有时我想插入的数字正好等于初始集合中的其他数字,在这种情况下,我也想获取下一个数字

我不知道是应该使用树状图(因为树状图总是被排序的,但有一个键的开销)、LinkedList(因为输入和删除值变得更容易)还是简单的 ArrayList


我将这些数字载入内存并进行密集计算,我的平台是移动的,所以我想要一些高效的东西。有什么提示吗?到目前为止,我使用的是一个链表,虽然我没有遇到任何性能问题,但我希望加快这个过程

如果我正确理解了你的问题,你真的不必插入数字来获取下一个数字,因为你正在删除它。在这种情况下,您可以使用简单的ArrayList本身执行类似的操作,以获得良好的性能

List<Integer> lst = new ArrayList<>();
lst.add(1);
lst.add(2);
lst.add(4);
lst.add(8);
lst.add(15);

// Collections.sort(lst); // assuming list is sorted

// your key 
int key = 7;

// binary search to look for position where it would be inserted.
// if negative number does not exist so next number would be at ' -pos - 1' 
// if positive number exists so next number would be at 'pos + 1' 
int pos = Collections.binarySearch(lst, key);
pos = pos < 0 ? -pos - 1 : pos + 1;

// check for out of bounds as search for last number would give index out of bounds
pos = pos == lst.size() ? pos - 1 : pos;

// your next number
System.out.println(lst.get(pos));
List lst=new ArrayList();
第1条增补(1);
第1条增补(2);
第1条增补(4);
第1条增补(8);
第1条增补(15);
//集合。排序(lst);//假设列表已排序
//你的钥匙
int键=7;
//二进制搜索以查找插入位置。
//如果负数不存在,则下一个数字将位于“-pos-1”
//如果存在正数,则下一个数字将位于“位置+1”
int pos=Collections.binarySearch(lst,key);
pos=pos<0-pos-1:pos+1;
//检查是否超出范围,因为搜索最后一个数字会导致索引超出范围
pos=pos==lst.size()?pos-1:pos;
//你的下一个号码
系统输出打印项次(lst.get(pos));

如果我正确理解了你的问题,你真的不必插入号码来获取下一个号码,因为你正在删除它。在这种情况下,您可以使用简单的ArrayList本身执行类似的操作,以获得良好的性能

List<Integer> lst = new ArrayList<>();
lst.add(1);
lst.add(2);
lst.add(4);
lst.add(8);
lst.add(15);

// Collections.sort(lst); // assuming list is sorted

// your key 
int key = 7;

// binary search to look for position where it would be inserted.
// if negative number does not exist so next number would be at ' -pos - 1' 
// if positive number exists so next number would be at 'pos + 1' 
int pos = Collections.binarySearch(lst, key);
pos = pos < 0 ? -pos - 1 : pos + 1;

// check for out of bounds as search for last number would give index out of bounds
pos = pos == lst.size() ? pos - 1 : pos;

// your next number
System.out.println(lst.get(pos));
List lst=new ArrayList();
第1条增补(1);
第1条增补(2);
第1条增补(4);
第1条增补(8);
第1条增补(15);
//集合。排序(lst);//假设列表已排序
//你的钥匙
int键=7;
//二进制搜索以查找插入位置。
//如果负数不存在,则下一个数字将位于“-pos-1”
//如果存在正数,则下一个数字将位于“位置+1”
int pos=Collections.binarySearch(lst,key);
pos=pos<0-pos-1:pos+1;
//检查是否超出范围,因为搜索最后一个数字会导致索引超出范围
pos=pos==lst.size()?pos-1:pos;
//你的下一个号码
系统输出打印项次(lst.get(pos));

收集类的性能分析

Collection          Number of Elements
Class               5000   10000   20000

HashSet               10      20      20
LinkedHashSet          0      10      20
Vector               661    2714   10936
ArrayList            651    2694   10676
LinkedList           762    3305   28122
TreeMap             1021   10256   52719
HashMap             1712   12629   60050
IdentityHashMap      391    1532    7000
WeakHashMap         1572  failed  failed
Hashtable           3145   21261   89103
从上述结果得出的结论:-

HashSet and LinkedHashSet maintained an almost constant performance level, while 
the number of elements doubled twice. They are perfectly designed for the search
operation.

Vector, ArrayList and LinkedList decreased their performance exponentially as the
number of elements doubles.

TreeMap, HashMap, IdentityHashMap and Hashtable decreased their performance 
exponentially as the number of elements doubles.

WeakHashMap is not reliable as mentioned in the JDK specification.
有关集合类性能的更多详细信息:-

HashSet and LinkedHashSet maintained an almost constant performance level, while 
the number of elements doubled twice. They are perfectly designed for the search
operation.

Vector, ArrayList and LinkedList decreased their performance exponentially as the
number of elements doubles.

TreeMap, HashMap, IdentityHashMap and Hashtable decreased their performance 
exponentially as the number of elements doubles.

WeakHashMap is not reliable as mentioned in the JDK specification.

  • 收集类的性能分析

    Collection          Number of Elements
    Class               5000   10000   20000
    
    HashSet               10      20      20
    LinkedHashSet          0      10      20
    Vector               661    2714   10936
    ArrayList            651    2694   10676
    LinkedList           762    3305   28122
    TreeMap             1021   10256   52719
    HashMap             1712   12629   60050
    IdentityHashMap      391    1532    7000
    WeakHashMap         1572  failed  failed
    Hashtable           3145   21261   89103
    
    从上述结果得出的结论:-

    HashSet and LinkedHashSet maintained an almost constant performance level, while 
    the number of elements doubled twice. They are perfectly designed for the search
    operation.
    
    Vector, ArrayList and LinkedList decreased their performance exponentially as the
    number of elements doubles.
    
    TreeMap, HashMap, IdentityHashMap and Hashtable decreased their performance 
    exponentially as the number of elements doubles.
    
    WeakHashMap is not reliable as mentioned in the JDK specification.
    
    有关集合类性能的更多详细信息:-

    HashSet and LinkedHashSet maintained an almost constant performance level, while 
    the number of elements doubled twice. They are perfectly designed for the search
    operation.
    
    Vector, ArrayList and LinkedList decreased their performance exponentially as the
    number of elements doubles.
    
    TreeMap, HashMap, IdentityHashMap and Hashtable decreased their performance 
    exponentially as the number of elements doubles.
    
    WeakHashMap is not reliable as mentioned in the JDK specification.
    


  • 添加到
    树集
    并使用
    迭代器
    “执行密集计算”非常模糊-因此不可预测,您应该使用什么样的映射/列表…密集计算是重复执行相同操作的混合,包括上面解释的操作以及DB插入,虽然我使用事务来执行DB操作(插入),但这些操作可能从几百到一千不等。有足够的代码,放在这里只会增加复杂性。是否要使用列表/地图作为主键的存储?不,我必须计算年龄并预测年龄,因此我获取当前年龄和开始/结束日期,并将其输入函数,然后在函数内我将当前年龄与下一个最高的元素进行比较(在列表/地图/集合中)例如,我的当前年龄是32岁,我将其插入列表,检查下一个最高值,即40。我将40输入日历函数,将日历设置为DOB/该事物的开始年龄,并获取该事物将变为40岁的日期。然后将该值插入DB。我计算了很多时间间隔ting.添加到
    树集
    并使用
    迭代器
    “执行密集计算”非常模糊-因此不可预测,您应该使用什么类型的映射/列表…密集计算是重复执行相同操作的混合,包括上面解释的操作以及DB插入,尽管我使用事务执行DB操作(插入)这些可能从几百到一千不等。代码足够多,放在这里只会增加复杂性。是否要使用列表/地图作为主键的存储?不,我必须计算年龄并预测年龄,因此我获取当前年龄和开始/结束日期,然后将其提供给函数,然后在函数内部进行比较e具有下一个最高元素的当前年龄(在列表/地图/集合中)例如,我的当前年龄是32岁,我将其插入列表,检查下一个最高值,即40。我将40输入日历函数,将日历设置为DOB/该事物的开始年龄,并获取该事物将变为40岁的日期。然后将该值插入DB。我计算了很多时间间隔ting.到目前为止,这是一个很好的开始,我希望这个答案清楚,@user2822178在做什么……老实说,我还是不明白……(我有很多猜测,但不知道他真正(我的意思是真的)想要什么…)@MartinFrank谢谢你。连我都不知道需要什么啊哈-也谢谢,你+1-你做到了^我很高兴你能提供帮助!@MartinFrank很高兴帮助:)再次感谢:)坦斯克的伙计们,如果我能