• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Streams. findAny signature.

 
Greenhorn
Posts: 29
Google Web Toolkit jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm studying Chapter 4 of the Sybex book Oracle OCP. All is very good presented. I have anyway problems to understand something.

Why is the signature of findAny/All/First so:
boolean anyMatch(Predicate <? super T> predicate)

I would expect a in order to be able to read (according the get/put principle) the properties of the generic parameter.

Could anyone help me?

Thank you so much in advance!
Greetings
Fabio

 
Saloon Keeper
Posts: 13280
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fabio,

The anyMatch() method doesn't care what the exact type is, as long as the predicate can *consume* a T.

For instance, let's say you have a Stream<BigDecimal> and a Stream<AtomicLong>, and you want to check if both of these streams contain a value that is larger than 50. You could write the following predicate for both:

You can pass largerThanFifty to both streams and it would work, because the first accepts a Predicate<? super BigDecimal> and the second accepts a Predicate<? super AtomicLong>, and Predicate<Number> fits both.

There is a mnemonic to remember this. PECS: Producer extends, consumer super. A Predicate is a consumer of T (that is, it only uses T as method parameters, not as return types), so it uses the super keyword to specify a lower bound. When you need a producer of T, (a type that only returns T, and doesn't use it for method parameters), then you use the extends keyword.
 
Fabio Salvi
Greenhorn
Posts: 29
Google Web Toolkit jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo Stephan,

super!! Thank you!!! :-)

Fabio
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic