Skip to content
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

Merged
merged 2 commits into from
Nov 14, 2023
Merged

Hw4 #6

merged 2 commits into from
Nov 14, 2023

Conversation

bifidok
Copy link
Owner

@bifidok bifidok commented Oct 25, 2023

No description provided.

@github-actions
Copy link

Code Coverage

Overall Project NaN% NaN% 🍏

There is no coverage information present for the Files changed

@bifidok bifidok changed the title tasks and tests done Hw4 Oct 28, 2023
return list.stream()
.collect(Collectors.groupingBy(Animal::sex, Collectors.counting()))
.entrySet().stream()
.max((o1, o2) -> (int) (o1.getValue() - o2.getValue())).get().getKey();

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();

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) {

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();

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();

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();

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();

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))

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();

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно попробовать сделать через flatMap.

@bifidok bifidok merged commit 9c7be54 into main Nov 14, 2023
2 checks passed
@bifidok bifidok deleted the hw4 branch November 21, 2023 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants