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

Impact Mapping

Making a big impact with software products and projects - Gojko Adzic

Before I read this book, I was on Impact Mapping workshop by Krzysztof Jelski on Agile By Example. I knew the concept. I practiced it in my company in one internal project. I am going to use it in next project with client. 

But now. About the book. The book is very good. The knowledge in this book is very concise. Which was good for me, but may be problematic for person who does not know the topic, because important parts may be missed. Because the book has a lot of condensed knowledge, I’m going to read it again.

I found things not directly related to impact mapping. For example chapter about iterative and incremental software building process. I liked chapters, which were abstracts of other peoples thoughts, for example Tom Gilb about metrics. It is an advantage.

I like the idea that the concept of impact maps has been described without going into unnecessary detail. This book is some kind of guideline, blueprint. And this is good. 

And the most important chapter for me was “Typical mapping mistakes”. It allowed me to find a place, where I do something bad (I was unaware of it). 

Apart from book. I asked myself, why we do impact maps? One of the main reasons is to create good channel of communication. To create a big picture view for technical and business people. Second reason is to decide, what should be built to achieve our goal, to reduce waste and over-engineered solutions. 

I strongly recommend this book and to try this technique. 

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