Sorting ArrayList with Lambda in Java 8
Solution 1
For strings this would work
arrayList.sort((p1, p2) -> p1.compareTo(p2));
Solution 2
Are you just sorting String
s? If so, you don't need lambdas; there's no point. You just do
import static java.util.Comparator.*;
list.sort(naturalOrder());
...though if you're sorting objects with a String
field, then it makes somewhat more sense:
list.sort(comparing(Foo::getString));
Solution 3
Use list.sort(String::compareToIgnoreCase)
Using list.sort(String::compareTo)
or list.sort(Comparator.naturalOrder())
will give incorrect (ie. non-alphabetical) results. It will sort any upper case letter before all lower case letters, so the array ["aAAA","Zzz", "zzz"]
gets sorted to ["Zzz", "aAAA", "zzz"]
Solution 4
Suppose you have List of names(String) which you want to sort alphabetically.
List<String> result = names.stream().sorted(
Comparator.comparing(n->n.toString())).collect(Collectors.toList());
its working perfectly.
Solution 5
In functional programming, you're not using the old objects to operate on them, but creating the new one in such a fashion:
list.stream().sorted().map(blah-blah).filter(...)...
Related videos on Youtube
Comments
-
Jeef almost 2 years
Could somebody show me a quick example how to sort an
ArrayList
alphabetically in Java 8 using the new lambda syntax.-
Alexis C. almost 10 yearsI suggest you to read this: docs.oracle.com/javase/tutorial/java/javaOO/…
-
-
Brian Goetz almost 10 yearsBetter: arrayList.sort(String::compareTo)
-
skiwi almost 10 yearsThis is incorrect, as now the stream no longer consists of the old elements, but of the mapped and filtered version. Consider a bank account which you want to sort by person name: If you do it like you suggest, then you start of with a stream of bank accounts, and end up with a stream of person names, while you want to end up with a stream of bank accounts again.
-
Dmitry Ginzburg almost 10 yearsGenerally, when you write your programs in FP-style, you don't need to iterately save the results. So, this all is a new list(s): list.stream().sorted() is not sorting the old list, but creating the new one.
-
skiwi almost 10 yearsAnd that list has just became useless as you only have the person's name left (in my example), and you cannot reference it back (directly) to their bank account anymore.
-
skiwi almost 10 yearsAlso, possibly a source of your confusion (nothing to blame), is that the OP requested a method to sort a list, yet you only return an intermediary stream, you also will need to store it at some point, which you omitted from your answer. By trying to implement that, you may see the issue yourself aswell.
-
Dmitry Ginzburg almost 10 yearsIf you really want to store the result in the same list, you can do something like:
list = list.stream().sort().collect(Collectors.toList());
, but again, author was requesting something in Java 8 style, and that's in Java 8 style. -
skiwi almost 10 yearsYou have now omitted specifying the sort-operator, please first get a working example, then update your answer with the working example.
-
Dmitry Ginzburg almost 10 years@skiwi there's no need to specify the sort operator as OP wanted to sort
String
s in lexicographic order, it's the default behavior. -
Holger almost 10 yearsEven better:
arrayList.sort(Comparator.naturalOrder())
-
UTF_or_Death about 8 yearsNote that both
.sort(String::compareTo) and .sort(Comparator.naturalOrder())
will sort all upper case letters before any lower case letters. Usually what you want is.sort(String::compareToIgnoreCase)
-
River over 6 yearsThe
Comparator
is entirely unneeded. -
Shahzad over 3 yearsSticking to functional programming, for (Product p : list) { System.out.println(p.id); } can be written as list.stream().forEach(System.out::println);