• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 14490
325
  • 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.
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic