forked from igor-baiborodine/java-various-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchDataExample.java
More file actions
113 lines (92 loc) · 3.29 KB
/
SearchDataExample.java
File metadata and controls
113 lines (92 loc) · 3.29 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.kiroule.ocpupgradejava8.topic4_2;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
/**
* @author Igor Baiborodine
*/
public class SearchDataExample {
public static void main(String... args) {
List<FuturamaCharacter> characters = Arrays.asList(
new FuturamaCharacter("Bender", "Rodriguez", "robot"),
new FuturamaCharacter("Philip", "Fry", "human"),
new FuturamaCharacter("Turanga", "Leela", "mutant"),
new FuturamaCharacter("Zapp", "Brannigan", "human"));
System.out.println("\nAll hashcodes:");
characters.stream()
.mapToInt(FuturamaCharacter::hashCode)
.sorted()
.forEach(System.out::println);
OptionalInt firstNegativeHashcode = characters
.stream()
.mapToInt(FuturamaCharacter::hashCode)
.sorted()
.findFirst();
System.out.println("\nFirst negative hashcode: " + firstNegativeHashcode);
OptionalInt anyHashcode = characters
.stream()
.mapToInt(FuturamaCharacter::hashCode)
.filter(hc -> hc != firstNegativeHashcode.getAsInt())
.findAny();
System.out.println("Any hashcode except the first negative hashcode: "
+ (anyHashcode.isPresent() ? anyHashcode.getAsInt() : "Not found"));
System.out.println("\nFuturama characters:");
characters.forEach(System.out::println);
boolean containsRobot = characters
.stream()
.anyMatch(c -> c.getSpecies().equals("robot"));
System.out.println("\nCharacters contain a robot: " + containsRobot);
boolean containsAlien = characters
.stream()
.noneMatch(c -> c.getSpecies().equals("alien"));
System.out.println("Characters do not contain an alien: " + containsAlien);
boolean containsOnlyHumans = characters
.stream()
.allMatch(FuturamaCharacter::isHumanSpecies);
System.out.println("Characters contain only humans: " + containsOnlyHumans);
}
}
class FuturamaCharacter {
private String firstName;
private String lastName;
private String species = "human";
public FuturamaCharacter(String firstName, String lastName, String species) {
this.firstName = firstName;
this.lastName = lastName;
this.species = species;
}
public String getSpecies() {
return species;
}
public boolean isHumanSpecies() {
return "human".equals(species);
}
@Override
public String toString() {
return firstName + " " + lastName + " [" + species + "]";
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FuturamaCharacter character = (FuturamaCharacter) o;
if (firstName != null ? !firstName.equals(character.firstName) : character.firstName != null) {
return false;
}
if (lastName != null ? !lastName.equals(character.lastName) : character.lastName != null) {
return false;
}
return !(species != null ? !species.equals(character.species) : character.species != null);
}
@Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + (species != null ? species.hashCode() : 0);
return result;
}
}