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!
Author by
saurabh suman
Updated on October 11, 2020Comments
-
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 over 7 yearsunfortunately, 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 over 7 yearsI think you mean
forEachOrdered
, notforEach
. -
Holger over 7 yearsThere is no reason to copy the result of
Arrays.asList
to anArrayList
; the methodArrays.asList
returns aList
. In either case, the list is mutable and doesn’t need a stream detour likelist = list.stream().sorted().collect(Collectors.toList());
a simplelist.sort(null);
will sort the list in-place. -
Holger over 7 years@Jude: do you know the difference between calling
list.stream().sorted()
and callinglist.sort(…)
? The custom comparator might be obsolete, still, this answer is correct. -
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 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)
orlist.sort(Comparator.naturalOrder())
… -
Jude Niroshan over 7 years@Holger yes.
Comparator.naturalOrder()
is to the point.