Sorting a list with stream.sorted() in Java

前端 未结 6 1022
无人及你
无人及你 2020-11-27 05:32

I\'m interested in sorting a list from a stream. This is the code I\'m using:

list.stream()
    .sorted((o1, o2)->o1.getItem().getValue().compareTo(o2.get         


        
6条回答
  •  天涯浪人
    2020-11-27 05:52

    This is a simple example :

    List citiesName = Arrays.asList( "Delhi","Mumbai","Chennai","Banglore","Kolkata");
    System.out.println("Cities : "+citiesName);
    List sortedByName = citiesName.stream()
                    .sorted((s1,s2)->s2.compareTo(s1))
                            .collect(Collectors.toList());
    System.out.println("Sorted by Name : "+ sortedByName);
    

    It may be possible that your IDE is not getting the jdk 1.8 or upper version to compile the code.

    Set the Java version 1.8 for Your_Project > properties > Project Facets > Java version 1.8

提交回复
热议问题