0

I want to display only 12 sorted elements from myList. If the size is less than 12, say 5, or 3, or 1, still I need to loop and display only those available items.

Below is my code:

public class JavaApplication {

    public static void main(String[] args) {

        List<String> myList = new ArrayList<>();
        myList.add("20150830");
        myList.add("20141201");
        myList.add("20150716");
        myList.add("20151131"); 
        myList.add("20141101");
        myList.add("20150620");
        myList.add("20150301");
        myList.add("20150702");
        myList.add("20150511");

        Collections.sort(myList,Collections.reverseOrder());

        for(int i = 0; i < myList.size(); i++) {
            System.out.println(myList.get(i).toString());
        }
    }     
}
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
senthil raja
  • 79
  • 1
  • 1
  • 5

6 Answers6

2

Just add one more condition in your loop to restrict the loop for 12.

for(int i = 0; i < myList.size() &&  i < 12 ; i++){
    System.out.println(myList.get(i).toString());
}
vefthym
  • 7,422
  • 6
  • 32
  • 58
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

This is a good use-case for streams.

myList.stream()
        .sorted(Comparator.<String>reverseOrder())
        .limit(12)
        .forEach(System.out::println);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You can try:

int maxValuesToDisplay=12;
int maxToIterate=(myList.size()<maxValuesToDisplay) ? myList.size() : maxValuesToDisplay;

for(int i = 0; i < maxToIterate; i++){
    System.out.println(myList.get(i).toString());
}
Erwan C.
  • 709
  • 5
  • 18
0

Use List.subList(int fromIndex, int toIndex);

public class JavaApplication {

    public static void main(String[] args) {

        List<String> myList = new ArrayList<>();
        myList.add("20150830");
        myList.add("20141201");
        myList.add("20150716");
        myList.add("20151131"); 
        myList.add("20141101");
        myList.add("20150620");
        myList.add("20150301");
        myList.add("20150702");
        myList.add("20150511");


        Collections.sort(myList,Collections.reverseOrder());
        int a = myList.size();
        List subList = null;
        if(a<12)
        subList = myList.subList(0,a);
        else subList = myList.subList(0,12);
       //Print sublist now 

 }     
    }
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
0

You could use the Math.min() function and iterate the minimum of 12 and the list-size.

for(int i = 0; i < Math.min(myList.size(), 12); i++){
    System.out.println(myList.get(i).toString());
   }
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • 4
    This is not optimal as this compute the min on each loop. – Vouze Aug 12 '15 at 09:37
  • list.size() is O(1) (see http://stackoverflow.com/questions/6540511/time-complexity-for-java-arraylist) and Math.min(num, num) is hardly going to add any significant overhead. – Mark Fisher Aug 12 '15 at 10:12
  • Also Math.min() is same as the `<` operator, has no complexity. Have a look at [this](http://stackoverflow.com/questions/2103606/which-one-is-faster-in-java-1-math-maxa-b-2-abab). ThIs keeps the for statement clean. – Uma Kanth Aug 12 '15 at 10:20
  • Except this is an object oriented language and you don't know what is behind "size()". Anyway for optimization, always get out of the loops invariants you can compute. – Vouze Aug 12 '15 at 14:20
0

You can use TreeSet :

public static void main(String[] args) {
    Set<String> myList = new TreeSet<>();
    myList.add("20150830");
    myList.add("20141201");
    myList.add("20150716");
    myList.add("20151131");
    myList.add("20141101");
    myList.add("20150620");
    myList.add("20150301");
    myList.add("20150702");
    myList.add("20150511");

    int i = 0;
    for(String s : myList){
        System.out.println(s);
        i++;
        if(i >= 5) {
            break;
        }
    }
}

And for reverse order :

    public static void main(String[] args) {
        TreeSet<String> myList = new TreeSet<>();
        myList.add("20150830");
        myList.add("20141201");
        myList.add("20150716");
        myList.add("20151131");
        myList.add("20141101");
        myList.add("20150620");
        myList.add("20150301");
        myList.add("20150702");
        myList.add("20150511");

        Iterator<String> it = myList.descendingIterator();
        int i = 0;

        while(it.hasNext()) {
            String s = it.next();
            System.out.println(s);
            i++;
            if (i >= 5) {
                break;
            }
        }
    }
Vouze
  • 1,678
  • 17
  • 10