-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.java
More file actions
193 lines (169 loc) · 6.19 KB
/
Copy pathAnimal.java
File metadata and controls
193 lines (169 loc) · 6.19 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Optional;
import java.util.Set;
import java.util.HashSet;
public record Animal(
String name,
Type type,
Sex sex,
int age,
int height,
int weight,
boolean bites
) {
enum Type {
CAT, DOG, BIRD, FISH, SPIDER
}
enum Sex {
M, F
}
public int paws() {
return switch (type) {
case CAT, DOG -> 4;
case BIRD -> 2;
case FISH -> 0;
case SPIDER -> 8;
};
}
//1
public List<Animal> sortByHeight(List<Animal> animals) {
return animals.stream()
.sorted(Comparator.comparingInt(Animal::height))
.toList();
}
//2
public List<Animal> sortByWeightAndSelectK(List<Animal> animals, int k) {
return animals.stream()
.sorted((a1, a2) -> Integer.compare(a2.weight(), a1.weight()))
.limit(k)
.toList();
}
//3
public Map<Type, Long> countAnimalsByType(List<Animal> animals) {
return animals.stream()
.collect(Collectors.groupingBy(Animal::type, Collectors.counting()));
}
//4
public Optional<Animal> findAnimalWithLongestName(List<Animal> animals) {
return animals.stream()
.max(Comparator.comparingInt(a -> a.name().length()));
}
//5
public String moreMalesOrFemales(List<Animal> animals) {
long males = animals.stream().filter(a -> a.sex() == Sex.M).count();
long females = animals.size() - males;
return males > females ? "Males" : females > males ? "Females" : "Equal";
}
//6
public Map<Type, Animal> heaviestAnimalPerType(List<Animal> animals) {
return animals.stream()
.collect(Collectors.groupingBy(Animal::type,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingInt(Animal::weight)),
opt -> opt.orElse(null)
)));
}
//7
public Optional<Animal> findLightestAnimal(List<Animal> animals) {
return animals.stream()
.min(Comparator.comparingInt(Animal::weight));
}
//8
public Optional<Animal> heaviestAnimalBelowKcm(List<Animal> animals, int k) {
return animals.stream()
.filter(a -> a.height() < k)
.max(Comparator.comparingInt(Animal::weight));
}
//9
public int totalNumberOfLegs(List<Animal> animals) {
return animals.stream()
.mapToInt(Animal::paws)
.sum();
}
//10
public List<Animal> animalsWithAgeMismatch(List<Animal> animals) {
return animals.stream()
.filter(a -> a.age() != a.height())
.toList();
}
//11
public List<Animal> bitingAnimalsAbove100cm(List<Animal> animals) {
return animals.stream()
.filter(a -> a.bites() && a.height() > 100)
.toList();
}
//12
public long countAnimalsWithWeightGreaterThanHeight(List<Animal> animals) {
return animals.stream()
.filter(a -> a.weight() > a.height())
.count();
}
//13
public List<Animal> animalsWithNamesMoreThanTwoWords(List<Animal> animals) {
return animals.stream()
.filter(a -> a.name().split(" ").length > 2)
.toList();
}
//14
public boolean hasDogTallerThanK(List<Animal> animals, int k) {
return animals.stream()
.anyMatch(a -> a.type() == Type.DOG && a.height() > k);
}
//15
public Map<Type, Integer> totalWeightPerTypeAge1to10(List<Animal> animals) {
return animals.stream()
.filter(a -> a.age() >= 1 && a.age() <= 10)
.collect(Collectors.groupingBy(Animal::type, Collectors.summingInt(Animal::weight)));
}
//16
public List<Animal> sortByTypeThenSexThenName(List<Animal> animals) {
return animals.stream()
.sorted(Comparator.comparing(Animal::type)
.thenComparing(Animal::sex)
.thenComparing(Animal::name))
.toList();
}
//17
public boolean spidersBiteMoreThanDogs(List<Animal> animals) {
long spiderBites = animals.stream()
.filter(a -> a.type() == Type.SPIDER && a.bites())
.count();
long dogBites = animals.stream()
.filter(a -> a.type() == Type.DOG && a.bites())
.count();
return spiderBites > dogBites;
}
//18
public Optional<Animal> findHeaviestFishInMultipleLists(List<List<Animal>> animalLists) {
return animalLists.stream()
.flatMap(List::stream)
.filter(a -> a.type() == Type.FISH)
.max(Comparator.comparingInt(Animal::weight));
}
//19
public Map<String, Set<String>> findAnimalsWithErrors(List<Animal> animals) {
return animals.stream()
.filter(a -> a.name() == null || a.name().isEmpty() || a.age() < 0 || a.weight() < 0)
.collect(Collectors.toMap(
Animal::name,
a -> {
Set<String> errors = new HashSet<>();
if (a.name() == null || a.name().isEmpty()) errors.add("Invalid name");
if (a.age() < 0) errors.add("Invalid age");
if (a.weight() < 0) errors.add("Invalid weight");
return errors;
}
));
}
//20
public Map<String, String> formatErrors(Map<String, Set<String>> errors) {
return errors.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> String.join(", ", entry.getValue())
));
}
}