Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Collectors.groupingBy() confusing behaviour in Java 8

 
Ranch Hand
Posts: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy everyone,

I was playing around with new Java 8 streams API, when I caught up with this compilation error. (I'm using IntelliJ IDEA)

Below is my Student.java



Here is my method method class:



This is the compilation error that I received,

Error:(36, 66) java: incompatible types: inference variable R has incompatible bounds
   equality constraints: java.util.Map<java.lang.String,java.util.List<org.chapter3.Student>>
   upper bounds: java.util.Map<java.lang.String,java.util.List<? super org.chapter3.Student>>,java.lang.Object



Thanks & Regards,
Jude
 
Ranch Hand
Posts: 51
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting question. I am NOT 100% certain about my answer, but it makes sense to me, and since this has been up for a day and I guess you are preparing for the exam, I will offer it anyway. You can either take my logic at face value, or you can wait for someone who is certain (though they may not necessarily be correct!) to validate it (I do advise the latter) :-)

I presume the reason you are surprised by this is that you thought because lower bounded wildcards don't render corresponding assigned objects immutable, it should not cause the problem that it does? I think the issue here is nothing to do with those rules around immutability, but that there has to be equality on either side of the expression where generics are declared (or diamond operator must be used on the right hand side to allow Java to infer the types).

In other words, you could think of your code as doing something like the following:



Clearly this will not compile because the generic types on either side don't match. I appreciate that in your actual code you are not doing a normal assignment with generic types declared on the right hand side etc, i.e. you are using a stream, but the point is that Java still knows that the generic types in the resulting assignment don't match. The fact that there are different rules around immutability between different types of wildcard is irrelevant.
 
Theo van Kraay
Ranch Hand
Posts: 51
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. I have assumed that you know what to do to correct the compiler (i.e remove the wildcard and just declare Student as the generic type) and I have also assumed that your question relates to the confusion I elaborated on above (please correct me if I was wrong in those assumptions).
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic