开发者

Java: How to sort List of Lists by their size?

I have 9 different ArrayList and I want to have a list of the top 5.

I'm thinking of sorting those ArrayLists by their sizes.

Is it possible to do that? If so, how can I achieve that?


After a few try i finally got it working, just want to share it with everyone.

it will be better to get the size of the arraylist and add it to the big arraylist

// creates an ArrayList that holds ArrayLists 
List allTheLists = new ArrayList(); 
allTheLists.add(pbaustraliaList.size());
allTheLists.add(pbotherList.size()); 
allTheLists.add(pbunitedStatesList.size()); 
allTheLists.add(pbunitedKingdomList开发者_如何学C.size()); 
allTheLists.add(pbchinaList.size()); 
allTheLists.add(pbgermanyList.size()); 
allTheLists.add(pbindiaList.size()); 
allTheLists.add(pbjapanList.size()); 
allTheLists.add(pbsingaporeList.size()); 
Comparator comparator = Collections.reverseOrder();
Collections.sort(allTheLists,comparator);

//display elements of ArrayList    
System.out.println("ArrayList elements after sorting in descending order : ");    
for(int i=0; i<allTheLists.size(); i++) {     
    System.out.println(allTheLists.get(i));   
}


What you could do is the following:

// this List of lists will need to contain 
// all of the ArrayLists you would like to sort
List<ArrayList> allTheLists; 
Collections.sort(allTheLists, new Comparator<ArrayList>(){
    public int compare(ArrayList a1, ArrayList a2) {
        return a2.size() - a1.size(); // assumes you want biggest to smallest
    }
});

This will sort the list of lists by the length of each list. The first element in the sorted list will be the longest list, and the last one will be the shortest list.

Then, you can iterate through the first 5 lists to see what the top 5 were.

Some links for reference:

  • Sorting tutorial
  • Collections Javadoc
  • Comparator Javadoc

Depending on how you have your ArrayLists stored, the code to create a List<ArrayList> would look something like this:

// creates an ArrayList that holds ArrayLists
List<ArrayList> allTheLists = new ArrayList<ArrayList>();
allTheLists.add(yourList1);
allTheLists.add(yourList2);
...
allTheLists.add(yourList9);


you can do like this as well

public static <T> List<List<T>> sort(List<List<T>> list) {
        list.sort((xs1, xs2) -> xs1.size() - xs2.size());
        return list;
    }


The sort method that's available on a List needs a Comparator. That comparator can be created with the Comparator.comparing method, with additional special implementations including for extracting and comparing an int - Comparator.comparingInt.

import static java.util.Comparator.comparingInt;
...
List<List<Integer>> listOfLists = ...
listOfLists.sort(comparingInt(List::size));

List::size will map a List to an int (the size of the list) and use that to create a new Comparator that can be used for our sorting purposes.

If you want largest first

listOfLists.sort(comparingInt(List::size).reversed());

Dump the top 5 (switching over to Java 8 streams):

listOfLists.stream()
  .sorted(comparingInt(List::size).reversed())
  .limit(5)
  .forEachOrdered(System.out::println);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜