-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw4 #6
Conversation
Code Coverage
|
return list.stream() | ||
.collect(Collectors.groupingBy(Animal::sex, Collectors.counting())) | ||
.entrySet().stream() | ||
.max((o1, o2) -> (int) (o1.getValue() - o2.getValue())).get().getKey(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get без проверки у Optional не надо делать. У Optional есть для этого метод map.
.max((o1, o2) -> (int) (o1.getValue() - o2.getValue()))
.map(Map.Entry::getKey).orElse(null);
return list.stream() | ||
.collect(Collectors.groupingBy(Animal::sex, Collectors.counting())) | ||
.entrySet().stream() | ||
.max((o1, o2) -> (int) (o1.getValue() - o2.getValue())).get().getKey(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше использовать Comparator.comparingLong, иначе можно получить ошибки при потере точности.
)); | ||
} | ||
|
||
public static Animal getKOldest(List<Animal> list, int k) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
На пустом списке будет ArrayIndexOutOfBoundsException.
public static Animal getKOldest(List<Animal> list, int k) { | ||
var sortedList = list.stream() | ||
.sorted(Comparator.comparingInt(Animal::age).reversed()) | ||
.limit(k + 1).toList(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Операция toList должна быть на новой строке.
|
||
public static Integer countAllPaws(List<Animal> list) { | ||
return list.stream() | ||
.mapToInt(Animal::paws).sum(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Операция sum должна быть на новой строке.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Операция sum должна быть на новой строке.
public static boolean isSpiderBitesOftenThanDog(List<Animal> list) { | ||
long spiderBitesCount = list.stream() | ||
.filter(s -> s.type().equals(Animal.Type.SPIDER)) | ||
.filter(Animal::bites).count(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Операция count должна быть на новой строке.
|
||
public static boolean isSpiderBitesOftenThanDog(List<Animal> list) { | ||
long spiderBitesCount = list.stream() | ||
.filter(s -> s.type().equals(Animal.Type.SPIDER)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В стримах тоже лучше использовать понятные имена переменных. В данном кейсе, animal.
for (List<Animal> subList : list) { | ||
Animal heavierFishInSubList = subList.stream() | ||
.filter(o -> o.type().equals(Animal.Type.FISH)) | ||
.max(Comparator.comparing(Animal::weight)).get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get без проверки у Optional не надо делать.
return spiderBitesCount > dogBitesCount; | ||
} | ||
|
||
public static Animal getHeavierFish(List<Animal>... list) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно попробовать сделать через flatMap.
No description provided.