• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics usage for accepting FunctionalInterface as parameters in the methods of ConcurrentHashMap

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I was looking at the Java 8 features.

While I was looking at the examples of Function and BiFunction usage in ConcurrentHashMap, I came across below two methods:
   
And:

Why is Generics used the way it is used?
Somewhere we have "? super" and somewhere we have "? extends". On what basis these parameters are decided?
Why can't all be "? super", or all be "? extends"?

And to add to this, I do understand how generics work and why we have "? super" or "? extends". But just curious to know why is it implemented that ways?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple: the first two generic type arguments capture the type of things you put IN the function, while the last type argument captures the type of thing you get OUT the function.

A BiFunction<T, U, R> is a consumer of T and U, and it's a producer of R. Remember the mnemonic PECS: Producer extends; consumer super.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic