Java 8 Comparator – How to sort a List

In this article we’re going to see several examples on how to sort a List in Java 8.

1. Sort a List of String alphabetically

By purpose we’ve written London with “L” in low-case to better highlight difference between Comparator.naturalOrder() that returns a Comparator that sorts by placing capital letters first and String.CASE_INSENSITIVE_ORDER that returns a case-insensitive Comparator.

Basically in Java 7 we were using Collections.sort() that was accepting a List and, eventually, a Comparator –  in Java 8 we have the new List.sort() that accepts a Comparator.

2. Sort a List of Integer

3. Sort a List by String field

Let’s suppose we’ve our Movie class and we want to sort our List “by title”. We can use Comparator.comparing() and pass a function that extracts the field to use for sorting – title – in this example.

Output will be:

As you’ve probably noticed we haven’t passed any Comparator but the List is correctly sorted. That’s because title – the extracted field – is a String and String implements Comparable interface. If you peek at Comparator.comparing() implementation you will see that it calls compareTo on the extracted key.

4. Sort a List by double field

In a similar way we can use Comparator.comparingDouble() for comparing double value. In the example we want to order our List of Movie by rating, from the highest to the lowest.

We used reversed function on the Comparator in order to invert default natural-order that is from lowest to highest. Comparator.comparingDouble() uses under the hood.

If you need to compare int or long you can use comparingInt() and comparingLong() respectively.

5. Sort a List with custom Comparator

In the previous examples we haven’t specified any Comparator since it wasn’t necessary but let’s see an example in which we define our own Comparator. Our Movie class has a new field – “starred” – set using the third constructor parameter. In the example we want to sort the list so that we have starred movie at the top of the List. 

Result will be:

We can, of course, use Lambda expression instead of Anonymous class as follow:

And we can also use again Comparator.comparing():

In the latest example Comparator.comparing() takes as first parameter the function to extract the key to use for sorting and a Comparator as second parameter. This Comparator uses the extracted keys for comparison, star1 and star2 are indeed boolean and represents m1.getStarred() and m2.getStarred() respectively.

6. Sort a List with chain of Comparator

In the latest example we want to have starred movie at the top and then sort by rating.

And output is:

As you’ve seen we first sort by starred and then by rating – both reversed since we want highest value and true first.

Java 8 Optional – Replace your get() calls

Optional class were introduced in order to prevent NullPointerException, but method get() used to retrieve the value inside the Optional might still throw a NoSuchElementException.

Different name, same issue?

Calling get() without checking that value is actually present it’s a bug. So we should always write something like that in order to use get().

But are Optional really meant to be used in this way? No.

Writing block of isPresent/get it’s not so different from writing a classic null check.

Let’s see how we can really benefit from Optional object.

1. Optional orElse example

It returns the value if is present, or the other specified otherwise.

Let’s see an example:

As you can see we haven’t called get() and we’ve made the code easier and more readable compared to the isPresent/get version:


2. Optional orElseThrow example

It returns the value if is present, or throws the specified exception otherwise.


3. Optional filter example

filter() is useful to specify other conditions on our object. It returns an Optional containing the value if is not empty and satisfy the specified predicate, an empty Optional otherwise.

In this example we want that the name not only is different from null but also that is not empty or made of only empty spaces.

And those are the tests for the null and the empty name:

4. Optional ifPresent example

IfPresent, that it’s different from isPresent, accept a function, a Consumer, and executes it only if the value is present.

So instead of writing something like:

You can write:

or if you prefer:

But let’s have a look to a proper example.

We define a Pojo class, useful also for the following examples, that represents a Loyalty card.

We want to add 3 points to the loyalty card if loyalty card is actually present.

Node: In the following example we’re going to use Mockito to mock LoyaltyCard class. Don’t worry if you are not familiar with Mockito, I’ll add some comments to the code.


5. Optional map example

map() it’s a method that we use to transform an input in a different output. In this case nothing changes except that the map operation will be executed only if the value is actually present, otherwise it returns an empty Optional.

In this example we want to retrieve the number of points of our loyalty card if we have it, otherwise number of point will return 0.

6. Optional flatMap example

flatMap() it’s really similar to map() but when output is already an Optional it doesn’t wrap it with another Optional. So instead of having Optional<Optional<T>> if will just return Optional<T>.

Let me clarify it using an example. Let’s define a new class, called Gift.

And let’s define a new method to our LoyaltyCard class that returns an Optional containing the last Gift chosen. Since we are going to mock the result of this method, we don’t really care about its implementation.

We can now create a mocked Gift with name “Biography of Guybrush Threepwood”, put it into an Optional and make getLastGift return it. So if we write:

Output will be an Optional<Optional<Gift>> that is not what we want, so flatMap will unwrap this double level and leave only an Optional<Gift>.

Writing this solution by using isPresent/get would have meant using a nested if: one for check that card was present and another of checking the gift. Harder to read, easier to fail.

7. Optional ifPresentOrElse ?

Unfortunately this is yet to come 🙂 It will be available in Java 9.

Until then we have to write something like:

There are cases in which you are allowed to use get() and isPresent() but use them with a grain of salt.



Short Tutorial By Example

Java 8 Stream – From List to Map

An example to convert a List<?> to a Map<K,V> using Java 8 Stream.

Java 8 – Collectors.toMap()

Let’s define a Pojo class:

In the first example we convert a List<Person> in a Map<String, Person> that has email as key and the object itself as value.

Output will be:

Or using lambda:


Let’s break this out

First of all we create a Stream of Person from the List<Person> defined.

Then we collect this stream in a Map. Java 8 helps us to define the needed Collector by providing us the method: Collectors.toMap().

Collectors.toMap() takes two functions – one for mapping the key and one for the value – and returns a Collector that accumulates elements into a Map.

Since we are working with Stream of Person – our input it’s an object Person.

We have chosen the email as key, so that’s function that given the input – Person – returns its email:

and then the object itself as value, so it’s just an identity function:

These are the parameters for toMap.

Another example

Given a List<Person> we want to create a Map<String, Integer> that contains the name as key and the age as value.

We just need to change the two parameters of the Collectors.toMap, by specifying:

So the code will be:


If you are a good observer you may have noticed that the order hasn’t been respected. That’s because the default implementation used by toMap is the HashMap that does not guarantee the order of the map.


Java 8 – Collectors.toMap with a LinkedHashMap


If we want to preserve the order we should use a LinkedHashMap instead of the HashMap.

Let’s try the previous example by passing a LinkedHashMap to Collectors.toMap()


We are using the definition of toMap that takes four parameters:

  • keyMapper – a mapping function to produce keys
  • valueMapper – a mapping function to produce values
  • mergeFunction – a merge function, used to resolve collisions between values associated with the same key
  • mapSupplier – a function which returns a new, empty Map into which the results will be inserted

We’ve already discussed the first two parameters.

In case of a collision we just want to throw an exception, so as third parameter we define that. In the example we used the same implementation of the static method throwingMerger defined in the class.

Fourth parameter it’s the one in which we define a function that returns our LinkedHashMap.

Java 8 – Convert List to String comma separated

Convert a List<String> in a String with all the values of the List comma separated in Java 8 is really straightforward. Let’s have a look how to do that.

In Java 8:

We simply can write String.join(..), pass a delimiter and an Iterable and the new StringJoiner will do the rest:

In case we are working with a stream we can write as follow and still have the same result:

Note: you can statically import if you prefer just typing “joining“.

In Java 7:

For the old times’ sake, let’s have a look to Java 7 implementation.

As you can see it’s much more verbose and easier to make mistakes like forgetting to remove the last comma. You can implement this in several ways – for example by moving the logic that removes last comma inside the for-loop – but none will be so explicative and immediate to understand as the declarative solution expressed in Java 8.

Focus should be on what you want to do – joining a List of String – not on how.


Java 8: Manipulate String before joining

Before joining you can manipulate your String as you prefer by using map() or cutting some String out by using filter().  I’ll cover those topics in further articles. Meanwhile, this a straightforward example on how to transform the whole String in upper-case before joining them.

Java 8: From List to upper-case String comma separated

If you  want to find out more about stream, I strongly suggest this cool video of Venkat Subramaniam.

Let’s play

The best way to learn it’s playing! Copy this class with all the implementations discussed and play with that. There is already a small test for each of them 🙂