-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicateIsEqualExample.java
More file actions
55 lines (44 loc) · 1.45 KB
/
PredicateIsEqualExample.java
File metadata and controls
55 lines (44 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package predefined_functional_interfaces.predicate;
/*
* Example demonstrating isEqual() method of Predicate
* It return a predicate that checks whether two arguments are equal based on Object.equal(Object,Object)
*/
import java.util.function.Predicate;
public class PredicateIsEqualExample {
public static void main(String[] args) {
Predicate<String> stringEquality = Predicate.isEqual("Anshuman");
System.out.println(stringEquality.test("Anshuman")); //true
System.out.println(stringEquality.test("Yuvraj")); //false
// equality check for non-primitive objects (equals methods needs to be overriden)
Predicate<Person> personEquality = Predicate.isEqual(new Person("Anshuman"));
System.out.println(personEquality.test(new Person("Anshuman"))); //ture
System.out.println(personEquality.test(new Person("Yuvraj"))); //false
}
}
class Person{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name) {
this.name = name;
}
//overriden equals method for custom equality check
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return name.equals(person.name);
}
}
/*
* Output:
* true
* false
* true
* false
*/