• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to translate this into Java 8 features?  RSS feed

 
John Freeman
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following simple class:


As well as the following driver program:


And I get the output as expected/required:

{Network={AA=NetworkUnavailable, AB=NetworkDown, AC=NetworkSlow}, Data={AA=DataCorrupted, BC=DataOutdated}}

My question is, how can I get the same logic using Java 8 features? I spent some time trying to wrap my mind around it, but I did not get anywhere.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can never remember how to create a Map, but let's try. You can get a Stream out of any Collection with its stream() method. There is also a parallelStream method, but I can see no point in parallelism for a five‑element List. So line 2 creates that List Stream.
You can collect the contents of a Stream into something with the collect() method (line 3). That takes a Collector instance as its parameter (overloaded method), and the easiest way to find a Collector is with a method of the Collectors class. This is a method that returns a Collector to create a Map.
Actually both the collect method and the Collector link show examples of the usage of groupingBy, which makes it easier to work out what to write .I am not sure whether that is what you really wanted, but it is as far as I can remember just at the moment. Somebody else will doubtess give a better suggestion.
The idiom ErrorMessage::getErrorType is called a method reference and you can read about it in that Java™ Tutorials link.

Note the different indentation from what you are used to: make the dot operators line up vertically.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a method that returns a Collector to create a Map. 

You could also use the function toMap in Collectors.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . You could also use the function toMap in Collectors.
Niiiiice

That's nice misspelt for anybody having difficulty with it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7961
143
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your pre-Java 8 code, there's no need to use a nested loop. You can achieve the same like this:

Since Java 8, Map has a computeIfAbsent() method. You can simplify the above to the following:

However, as Campbell and Knute have pointed out, and even more functional way can be achieved using collectors:
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think those checks and the filter for the error type being null can be omitted though. There's no such check in the original code; you probably misread the Map.get call on line 18.
 
John Freeman
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the responses everyone!

Campbell, your output was not quite what I was looking for, because my values for the outer map are Map<String, String> rather than List<ErrorMessage>, but thanks for your tip about the indentation style.

Knute, thanks for bringing up toMap and Stephan for the thorough response of the alternatives in Java 8, it has given me a much better understanding of how to use it fit a requirement, as well as for the optimization of my current code, it has definitely opened up my mind to put an effort into making my code more efficient. And finally, Rob you are right, the filter check is not needed in this scenario because all of the ErrorMessage objects in the stream are assumed to have a valid errorType String.

Looks like I need to book my books again for Java 8 as well as just keep practicing!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said somebody else would have a better suggestions, didn't I.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!