Java 8 Stream sorting List of String

42,520

Solution 1

Change this

list.stream().sorted();
System.out.println(list);

to something like

list.stream().sorted().forEachOrdered(System.out::println);

Your method is println the list (not the sorted stream). Alternatively (or additionally), you could shorten your initialization routine and re-collect the List like

List<String> list = new ArrayList<>(Arrays.asList("b","a","z","p"));
list = list.stream().sorted().collect(Collectors.toList());
System.out.println(list);

Which outputs (as you probably expected)

[a, b, p, z]

Solution 2

You should collect the result of the sorting and then assign it to your list.

    List<String> list = new ArrayList<String>();
    list.add("b");
    list.add("a");
    list.add("z");
    list.add("p");
    list = list.stream().sorted().collect(Collectors.toList());
    System.out.println(list);

Solution 3

If you want to have your sorted list.

Let's change this

list.stream().sorted();

to

list.sort((e1, e2) -> e1.compareTo(e2));

Hope this help!

Share:
42,520
saurabh suman
Author by

saurabh suman

Updated on October 11, 2020

Comments

  • saurabh suman
    saurabh suman over 3 years

    I am calling the sorted method on a stream. And the java doc says:

    "Sorted method returns a stream consisting of the elements of this stream, sorted according to natural order."

    But when I run the code below:

    List<String> list = new ArrayList<String>();
    list.add("b");
    list.add("a");
    list.add("z");
    list.add("p");
    list.stream().sorted();
    System.out.println(list);
    

    I am getting output as

    [b, a, z, p]
    

    Why am I not getting the output of a natural sort?

  • Jude Niroshan
    Jude Niroshan over 7 years
    unfortunately, this is not a acceptable answer. Because, the OP's way is indeed correct. You don't need to pass a custom comparator in order to get the natural ordering for a given stream of string objects. BTW; above 2 answers will point what has went wrong. Hope you could learn something from it :)
  • Louis Wasserman
    Louis Wasserman over 7 years
    I think you mean forEachOrdered, not forEach.
  • Holger
    Holger over 7 years
    There is no reason to copy the result of Arrays.asList to an ArrayList; the method Arrays.asList returns a List. In either case, the list is mutable and doesn’t need a stream detour like list = list.stream().sorted().collect(Collectors.toList()); a simple list.sort(null); will sort the list in-place.
  • Holger
    Holger over 7 years
    @Jude: do you know the difference between calling list.stream().sorted() and calling list.sort(…)? The custom comparator might be obsolete, still, this answer is correct.
  • Jude Niroshan
    Jude Niroshan over 7 years
    @Holger hi, the 1st one will just sort the elements in natural order. (using compareTo or obj.toString().compareTo() implementations). Ohh, man... It's a new default method added from Java 8 to List interface. Sorry, my bad. But, this answer, doesn't point out the fact that intermediate operations won't fire unless having a terminal operation.
  • Holger
    Holger over 7 years
    @Jude: even with a terminal operation, stream operations do not modify the source list. Yes, this answer lacks explanations, but provides a straight-forward solution. Though, I’d prefer list.sort(null) or list.sort(Comparator.naturalOrder())
  • Jude Niroshan
    Jude Niroshan over 7 years
    @Holger yes. Comparator.naturalOrder() is to the point.