Skip to content

Java02. ДЗ 04, Шевченко Илья#5

Open
Qwsafex wants to merge 10 commits into
masterfrom
Streams
Open

Java02. ДЗ 04, Шевченко Илья#5
Qwsafex wants to merge 10 commits into
masterfrom
Streams

Conversation

@Qwsafex

@Qwsafex Qwsafex commented Nov 28, 2016

Copy link
Copy Markdown
Owner

No description provided.

@sproshev sproshev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

надо исправить


// Вернуть поток из объектов класса 'clazz'
public static <R> Stream<R> filterIsInstance(Stream<?> s, Class<R> clazz) {
return s.filter(clazz::isInstance).map(o -> (R) o);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

clazz::cast

map1.compute(key, (k,v) -> (v == null) ? val : val + v);
});
return map1;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

см Collectors.groupingBy и Collectors.summingInt

@Test
public void findPrinterSingleAndEmpty(){
HashMap<String, List<String>> authors = new HashMap<>();
authors.put("Mike", Arrays.asList());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

все такие вызовы без аргументов должны быть заменены на Collections.emptyList, а с одним аргументом на Collections.singletonList. Они не выделяют дополнительной памяти (массива)

}

private void writeToFile(File file1, String... lines) throws FileNotFoundException {
PrintStream ps = new PrintStream(file1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

а закрыть?

@Qwsafex

Qwsafex commented Dec 6, 2016

Copy link
Copy Markdown
Owner Author

@sproshev Поправил

@sproshev sproshev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

10

.max(Comparator.comparing(entry -> entry.getValue()
.stream()
.mapToInt(String::length)
.sum())).get().getKey();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

лучше map(...::getKey).orElse(null), так не будет исключения, если compositions.isEmpty

return orders.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.reducing(0, Map.Entry::getValue, Integer::sum)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Collectors.summingInt(Map.Entry::getValue)

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