New methods in Map.Entry comparingByKey and comparingByValue

JDK 8 is still new and hot. I have explored some new methods in Map.Entry interface, to sort objects.

Image that we have following collection:

Initial data
1
2
3
4
5
Map map = new HashMap<>();
map.put("Kawasaki", 3);
map.put("Honda", 1);
map.put("Norton", 5);
map.put("Moto Guzzi", 2);

There are four new methods:

  • comparingByKey()
  • comparingByKey(Comparator<? super K> cmp)
  • comparingByValue()
  • comparingByValue(Comparator<? super K> cmp)

The method names are self-describing. If we pass different comparator, we can get different behaviour.

Comparing by value

If we want to sort by value with default behaviour (ascending), we can do something like this:

ComparingByValue
1
2
3
4
5
6
7
List<Map.Entry<String, Integer>> comparingByValue = map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toList());

comparingByValue.forEach(System.out::println);

As a result, we get a list sorted by values:

1
2
3
4
Honda=1
Moto Guzzi=2
Kawasaki=3
Norton=5

Comparing by key

If we want to sort by key with default behaviour (ascending), we can do something like this:

ComparingByKey
1
2
3
4
5
6
7
List<Map.Entry<String, Integer>> comparingByKey = map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());

comparingByKey.forEach(System.out::println);

As a result, we get a list sorted by keys alphabetically:

1
2
3
4
Honda=1
Kawasaki=3
Moto Guzzi=2
Norton=5

Comparing by key with comparator

If we want to sort by key length, we can pass function as a comparator,  Then we can achieve something like this:

ComparingByValue with comparator
1
2
3
4
5
6
7
List<Map.Entry<String, Integer>> comparingByValue = map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey((String s1, String s2) -> s1.length() - s2.length()))
.collect(Collectors.toList());

comparingByValue.forEach(System.out::println);

As a result, we get a list sorted by keys length:

1
2
3
4
Honda=1
Norton=5
Kawasaki=3
Moto Guzzi=2

Photo credit

Functional Programming in Java

Harnessing the Power of Java 8 Lambda Expressions

Venkat Subramaniam

My new year commitment was, that I read one book each month. I don’t predicted an unexpected events that may me slow down :( I do not give up. Next book review will be in 16’th of April.

About the book. I’ve read a beta 6 edition from 13 of January, but I think that final version is very similar to the this beta version. It is not the book about Java 8. It is a book about Lambda Expressions in JDK8. The title says that. In JDK8 there are for example changes in GC. I was missing at least one chapter or few pages about it. 

It was book easy to understand. I have Groovy and Scala experience, so there was nothing new to me. All the time I was mapping Java code to Scala code :). It was good for me. 

Next good thing is that it reads like a group of blog posts. We have some examples of imperative style, then we move to declarative or functional style. Size of chapters was appropriate for me to read before sleep. 

I didn’t like code examples (not in book, but downloadable code). Code examples was without tests and assertions. Just run and print output. I like BDD tests, and it will be great to read examples that are combined with BDD tests. 

I would recommend this book to a friend, because it is focused on good developer practices. 

Photo Credit