• 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

Predicate is equals method signature not clear

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.function.Predicate has a method isEquals with the following signature:

static <T> Predicate<T> isEqual(Object targetRef)

i do not understand the signature though. THe method returns Predicate<T> . i get that , however what is <T> Predicate<T> . why the additional <T> in the front?
 
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static <T> Predicate<T> isEqual(Object targetRef)
The <T> before the static defines a parametrized type at method level. It is a generic method.

Generic Methods at Oracle's Java documentation: https://docs.oracle.com/javase/tutorial/extra/generics/methods.html

java.util.ArrayList<E> is a generic class.
Within this class there are methods like: public boolean add(E e), where E is the type of the element.
 
Saloon Keeper
Posts: 13280
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This method exists for the sole purpose of creating a predicate that you can use in higher order functions. For instance, here's how you could check that a list of Strings contains a specific string, if the contains() method didn't exist:

Now, this would work even if isEqual() had the following declaration:

That's because Stream.anyMatch() accepts a Predicate<? super T>.

However, some libraries aren't that well written and instead of a Predicate<? super T>, they accept a Predicate<T>.

What would happen if Stream.anyMatch() accepted a Predicate<T>, AND Predicate.isEqual() returned a Predicate<Object>? The snippet I wrote at the very top wouldn't compile, because Predicate<String> is not assignable from Predicate<Object>.

Instead, even if anyMatch() was poorly written, it would work if the declaration of Predicate.isEqual() is as it is now, because it infers that the type that it needs to return is a Predicate<String>. That's why it uses a type parameter.
 
Ranch Hand
Posts: 344
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


(1) isEqual(Object targetRef)
The parameter declaration says thay you can pass the instance of any class to this method. All the classes in Java are subclasses of the Object class.

(2) <T> Predicate<T>
The T is a place holder for a specific type.  Normally, if you want to create a list designed to hold only instances of Dog, you would write List<Dog>.  However, in the context of isEqual() method, you have not way to know the class of the instance that will be passed to the method.  That means you have no way to know the class of instances that the predicate will be testing. Even so, the class can be inferred from the argument passed to the method. Therefore, the <T> before the Predicate<T> is Java's way of saying "the T will be replaced with the class of the argument passed to the method".
Now, if you do the floowing:

Java will work out the fact the dog was created from Dog.  Therefore, the isEqual() method will return type Predicate<Dog>. The T has been replaced with Dog. If you now try to use this predicate to test instances of Cat, the compiler will shoot out an error because a Cat class is not a Dog class.
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic