• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

a weird code with generics

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello together,

I was about to play with the Java 8 Map API. Following my code:



so far, so good. But the situation which makes me confused, is the following:

if I want to sort my Map and display the content of the map, I can write something like this:



eclipse Neon returns the following warning: Type safety: Unchecked cast from Object to Map.Entry<String, String>

Question 1: why do I have the warning?

to avoid the warning I changed the code like this, after have seen a similar code in internet:

Question 2: could somebody help me to decrypt the syntax used? In other words what the hell should this weird syntax mean?
Map.Entry.<String,String>comparingByKey().... e.g.  the diamond operator direct after the point, directly followed by a function.
Why is it necessary, to write it so komplex? Probably I have to learn something concerning the generics.

thanks for helping
 
Ranch Hand
Posts: 86
18
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The warning is caused by the fact, that you are calling reversed on the comparator -> the compiler will no longer infer the type arguments of the comparator based on the sorted call as another method call is added inbetween -> type parameter is erased to Object.
As a result you currently create a Comparator<Object> that casts its argument to Map.Entry, which leads to the warning. You can specify the type of the argument instead of casting to prevent the unchecked cast as following:

or, a bit shorter

In your second example you are explicitly specifying the type arguments of the called method (comparingByKey has <K extends Comparable<? super K>, V> as type parameters (and returns Comparator<Map.Entry<K, V>>) -> you are telling the method to use String as type for K and V). The reason why this is required is again the intermediate reversed call which makes type infering based on the sorted call impossible.
 
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer given by Tobias is excellent. Java is unable to infer the type, so you have to indicate it. You might however do this by other means, which, although less efficient, are worth considering:

You may declare the function on its own, which would make it reusable (it might be useful in some cases):



or you might parameterize the call to the comparing method:



The first example is probably the cleanest and the easiest to read. It has however a performance impact, since it forces the creation of an object to represent the function, which is not the case with the other examples.

The second example uses a parameterized method call, exactly like your example with the comparingByKey method. It is ugly, and has the drawback of probably not being so well known by Java developers, so it might make your code difficult to read to others. On the other hand, it might make them learn something ;-) Parameterized method calls are essential if you intend to do functional programming in Java.

 
Pierre-Yves Saumont
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possibility would be to use the following:



or you might call reverse later:



And if the keys are to be sorted ignoring case, you might simplify this further:




 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic