Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander committed Nov 1, 2023
1 parent b3d8783 commit d69ea91
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/main/java/edu/hw4/AnimalRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.hw4;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand All @@ -18,93 +20,98 @@ private AnimalRepository() {

public static List<Animal> sortByHeight(List<Animal> list) {
return list.stream()
.sorted((o1, o2) -> o1.height() - o2.height())
.sorted(Comparator.comparingInt(Animal::height))
.collect(Collectors.toList());
}

public static List<Animal> firstKSortedByWeight(List<Animal> list, int k) {
return list.stream()
.sorted(((o1, o2) -> o2.weight() - o1.weight()))
.sorted(((animal1, animal2) -> animal2.weight() - animal1.weight()))
.limit(k)
.collect(Collectors.toList());
}

public static Map<Animal.Type, Integer> getTypesCount(List<Animal> list) {
return list.stream()
.collect(Collectors.toMap(Animal::type, a -> 1, Math::addExact));
.collect(Collectors.toMap(Animal::type, animal -> 1, Math::addExact));
}

public static Animal getByLongestName(List<Animal> list) {
return list.stream()
.max((o1, o2) -> o1.name().length() - o2.name().length()).orElse(null);
.max(Comparator.comparingInt(animal -> animal.name().length())).orElse(null);
}

public static Animal.Sex getGreaterSex(List<Animal> list) {
return list.stream()
.collect(Collectors.groupingBy(Animal::sex, Collectors.counting()))
.entrySet().stream()
.max((o1, o2) -> (int) (o1.getValue() - o2.getValue())).get().getKey();
.max(Comparator.comparingLong(Map.Entry::getValue))
.map(Map.Entry::getKey).orElse(null);
}

public static Map<Animal.Type, Animal> getHeavierAnimalForEachType(List<Animal> list) {
return list.stream()
.collect(Collectors.toMap(Animal::type, a -> a,
.collect(Collectors.toMap(Animal::type, animal -> animal,
BinaryOperator.maxBy(Comparator.comparing(Animal::weight))
));
}

public static Animal getKOldest(List<Animal> list, int k) {
var sortedList = list.stream()
.sorted(Comparator.comparingInt(Animal::age).reversed())
.limit(k + 1).toList();
return sortedList.get(k - 1);
.limit(k + 1)
.toList();
return k > sortedList.size() ? null : sortedList.get(k - 1);
}

public static Optional<Animal> getHeavierBelowK(List<Animal> list, int k) {
return list.stream()
.filter(o -> o.weight() < k)
.filter(animal -> animal.weight() < k)
.max(Comparator.comparingInt(Animal::weight));
}

public static Integer countAllPaws(List<Animal> list) {
return list.stream()
.mapToInt(Animal::paws).sum();
.mapToInt(Animal::paws)
.sum();
}

public static List<Animal> getAllWhereAgeIsNotEqualToPawsCount(List<Animal> list) {
return list.stream()
.filter(o -> o.age() != o.paws())
.filter(animal -> animal.age() != animal.paws())
.collect(Collectors.toList());
}

public static List<Animal> getAllWhichBitesAndHigherHundredCM(List<Animal> list) {
return list.stream()
.filter(o -> o.bites())
.filter(o -> o.height() > HUNDRED_CENTIMETERS).collect(Collectors.toList());
.filter(animal -> animal.bites())
.filter(animal -> animal.height() > HUNDRED_CENTIMETERS)
.collect(Collectors.toList());
}

public static Integer countAnimalsWhereWeightLargerThanHeight(List<Animal> list) {
return Math.toIntExact(list.stream()
.filter(o -> o.weight() > o.height())
.filter(animal -> animal.weight() > animal.height())
.count());
}

public static List<Animal> getAllWhereNameHasMoreThanTwoWords(List<Animal> list) {
return list.stream()
.filter(o -> o.name().split(" ").length > 2)
.filter(animal -> animal.name().split(" ").length > 2)
.collect(Collectors.toList());
}

public static boolean containsDogHeightLargerThanK(List<Animal> list, int k) {
return list.stream()
.filter(o -> o.type().equals(Animal.Type.DOG))
.anyMatch(o -> o.height() > k);
.filter(animal -> animal.type().equals(Animal.Type.DOG))
.anyMatch(animal -> animal.height() > k);
}

public static Integer countWeightSumByAge(List<Animal> list, int ageFrom, int ageTo) {
return list.stream()
.filter(o -> o.age() >= ageFrom && o.age() <= ageTo)
.mapToInt(Animal::weight).sum();
.filter(animal -> animal.age() >= ageFrom && animal.age() <= ageTo)
.mapToInt(Animal::weight)
.sum();
}

public static List<Animal> sortByTypeThenSexThenName(List<Animal> list) {
Expand All @@ -117,29 +124,22 @@ public static List<Animal> sortByTypeThenSexThenName(List<Animal> list) {

public static boolean isSpiderBitesOftenThanDog(List<Animal> list) {
long spiderBitesCount = list.stream()
.filter(s -> s.type().equals(Animal.Type.SPIDER))
.filter(Animal::bites).count();
.filter(animal -> animal.type().equals(Animal.Type.SPIDER))
.filter(Animal::bites)
.count();
long dogBitesCount = list.stream()
.filter(d -> d.type().equals(Animal.Type.DOG))
.filter(Animal::bites).count();
.filter(animal -> animal.type().equals(Animal.Type.DOG))
.filter(Animal::bites)
.count();
return spiderBitesCount > dogBitesCount;
}

public static Animal getHeavierFish(List<Animal>... list) {
Animal heavierFish = null;
int maxWeight = -1;

for (List<Animal> subList : list) {
Animal heavierFishInSubList = subList.stream()
.filter(o -> o.type().equals(Animal.Type.FISH))
.max(Comparator.comparing(Animal::weight)).get();

if (heavierFishInSubList.weight() > maxWeight) {
heavierFish = heavierFishInSubList;
maxWeight = heavierFish.weight();
}
}
return heavierFish;
return Arrays.stream(list)
.flatMap(Collection::stream)
.filter(animal -> animal.type().equals(Animal.Type.FISH))
.max(Comparator.comparing(Animal::weight))
.orElse(null);
}

public static Map<String, Set<ValidationError>> getAnimalsWithBadRecords(List<Animal> list) {
Expand All @@ -150,8 +150,8 @@ public static Map<String, Set<ValidationError>> getAnimalsWithBadRecords(List<An
return Stream.of(typeFieldErrors, sexFieldErrors, numberVariablesFieldErrors)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(
e -> e.getKey(),
Collectors.mapping(e -> e.getValue(), Collectors.toSet())
entry -> entry.getKey(),
Collectors.mapping(entry -> entry.getValue(), Collectors.toSet())
));
}

Expand All @@ -163,7 +163,7 @@ public static Map<String, String> getAnimalsWithBadRecordsBetter(List<Animal> li
return Stream.of(typeFieldErrors, sexFieldErrors, numberVariablesFieldErrors)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(
e -> e.getKey(),
entry -> entry.getKey(),
Collectors.mapping(e -> e.getValue().property(), Collectors.joining(", "))
));
}
Expand Down

0 comments on commit d69ea91

Please sign in to comment.