简体   繁体   English

排序巨大的数组列表(ArrayList <Class> )基于Java中类成员的值

[英]Sorting huge Array List (ArrayList<Class>) based on values of class members in Java

There are two sub-problems. 有两个子问题。

1- Comparing two huge arraylists 1-比较两个巨大的数组列表

2- Sorting elements of arraylist based on the values of its object to achieve (1). 2-根据其对象的值对arraylist的元素进行排序,以实现(1)。

I have an ArrayList of objects of a class. 我有一个类的对象的ArrayList。 ie

Class X
{
    double x;
    double y;
    int sortVal;
}

ArrayList<X> alX = new ArrayList<X>(); //size = 10,000
ArrayList<Integer> myValue = new ArrayList<Integer>(); //size = 15

I want to check if myValue is present in sortVal. 我想检查myValue是否存在于sortVal中。

X ob = new X();
for(i=0;i<myValue.size();i++)
{
   for(j=0;j<alX.size();j++)
   {  
      ob = alX.get(j)
      **if (myValues.get(i) == ob.sortVal)**
   }
}

As the size of the arraylist 'alX' is huge, it takes high computation time. 由于数组列表“ alX”的大小很大,因此需要大量的计算时间。

I thought the better way would be to sort the elemets of ArrayList alX based on the values of sortVal of Class X. By sorting, once sortVal is greater than myValue, I can break from the loop. 我认为更好的方法是根据X类的sortVal值对ArrayList alX的元素进行排序。通过排序,一旦sortVal大于myValue,我就可以摆脱循环。

1) how can I sort the elements of arraylist alX based on the value 'sortVal'. 1)如何基于值“ sortVal”对arraylist alX的元素进行排序。

2) Is there a better approach, than sorting the arraylist, to compare the two values. 2)有没有比对数组列表排序更好的方法来比较两个值。 ie (myValues.get(i) == alX.ob.sortVal) (myValues.get(i)== alX.ob.sortVal)

[edit] Consider the values being, [edit]考虑值是,

ArrayList<X>:
x      : 1,1,1,2,3,5,4,5
y      : 2,4,6,4,4,6,2,1
sortVal: 10,20,30,10,10,20,30

ArrayList<Integer>:
myValue: 10,20,30

You could construct a Map<Integer, X> if sortVal values are unique or Map<Integer, Lists<X>> if they are not. 如果sortVal值唯一Map<Integer, X>可以构造Map<Integer, X>如果不是Map<Integer, X>可以构造Map<Integer, Lists<X>> This has a time complexity of O(n) instead or O(n * log n) which is the cost of doing a sort. 它的时间复杂度为O(n)或O(n * log n),这是进行排序的成本。


EDIT: This builds a MultiMap of keys and the set of objects for that key in one pass. 编辑:这将一次构建键的MultiMap和该键的对象集。

List<X> xs = ...
Map<Integer, Set<X>> mapBySortVal = new LinkedHashMap<>();
for(X x: xs) {
   Set<X> set = mapBySortVal.get(x.sortVal);
   if (set == null)
      mapBySortVal.put(x.sortVal, set = new LinkedHashSet<>());
   set.add(x);
}

for(Integer value: myValues) {
   Set<X> xs = mapBySortVal.get(value);
   if (xs != null) 
       // found some.
}

For your first question, you can sort a collection on anything you can think of by specifying the Comparator it should use (see Collections#sort ) 对于第一个问题,您可以通过指定应该使用的Comparator对可以想到的任何内容进行排序(请参阅Collections#sort

Since you named your field sortVal , I would guess instances of this class can be sorted based on that value and you might want to implement the Comparable interface for that class. 由于您将字段命名为sortVal ,因此我猜想可以根据该值对此类的实例进行排序,并且您可能想为该类实现Comparable接口。 That way, you can use Collections#sort without having to specify the Comparator 这样,您可以使用Collections#sort而不必指定Comparator

Google guava get very useful functions. Google番石榴具有非常有用的功能。 For ordering of arrays it provide com.google.common.collect.Ordering<T> . 对于数组的排序,它提供com.google.common.collect.Ordering<T>

For example: 例如:

Ordering<String> byLengthOrdering = new Ordering<String>() {
 public int compare(String left, String right) {
   return Ints.compare(left.length(), right.length());
 }
};
List<String> sorted = byLengthOrdering.reverse().sortedCopy(sourceList);

Depending on your requirement there are 2 approaches: Use Map, which orders its elements by natural order by default. 根据您的要求,有2种方法:使用地图,默认情况下按自然顺序对其元素进行排序。 But will take more memory. 但是会占用更多的内存。

OR 要么

Sort using comparator. 使用比较器排序。 Will take more time. 需要更多时间。

Just look at their docs for explanation. 只需查看他们的文档即可获得解释。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM