Collectig Data With Stream Flashcards

recipe for how to build a summary of the elements in the stream.

1
Q

count the number of dishes in the menu, using the

collector returned by the counting factory method:

A

long howManyDishes = menu.stream().collect(Collectors.counting());

You can write this far more directly as
long howManyDishes = menu.stream().count();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Suppose you want to find the highest-calorie dish in the menu.

A

Comparator dishCaloriesComparator =
Comparator.comparingInt(Dish::getCalories);
Optional mostCalorieDish =
menu.stream()
.collect(maxBy(dishCaloriesComparator));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Summeraization: you can find the total number of

calories in your menu list with

A

int totalCalories = menu.stream().collect(summingInt(Dish::getCalories));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

summarization: Calculate the Average

A

double avgCalories =

menu.stream().collect(averagingInt(Dish::getCalories));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is int SummaryStatistics?

A

For example, you can count the elements in the menu and obtain the sum, average, maximum, and minimum of the calories contained in each dish with a single
summarizing operation:

IntSummaryStatistics menuStatistics =
menu.stream().collect(summarizingInt(Dish::getCalories));

IntSummaryStatistics{count=9, sum=4300, min=120,
average=477.777778, max=800}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Joining Strings

A

The collector returned by the joining factory method concatenates into a single string,
all strings resulting from invoking the toString method on each object in the stream.

This means you can concatenate the names of all the dishes in the menu as follows:
String shortMenu = menu.stream().map(Dish::getName).collect(joining());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Collect vs. reduce

A

We’ve discussed reductions a lot in the previous chapter and this one. You may wonder
what the differences between the collect and reduce methods of the stream
interface are, because often you can obtain the same results using either method.
For instance, you can achieve what is done by the toList Collector using the
reduce method as follows:

Stream stream = Arrays.asList(1, 2, 3, 4, 5, 6).stream();
List numbers = stream.reduce(
new ArrayList(),
(List l, Integer e) -> {
l.add(e);
return l; },
(List l1, List l2) -> {
l1.addAll(l2);
return l1; });

This solution has two problems: a semantic one and a practical one. The semantic
problem lies in the fact that the reduce method is meant to combine two values and
produce a new one; it’s an immutable reduction. In contrast, the collect method is
designed to mutate a container to accumulate the result it’s supposed to produce.
This means that the previous snippet of code is misusing the reduce method,
because it’s mutating in place the List used as accumulator. As you’ll see in more
detail in the next chapter, using the reduce method with the wrong semantic is also
the cause of a practical problem: this reduction process can’t work in parallel,
because the concurrent modification of the same data structure operated by multiple
threads can corrupt the List itself. In this case, if you want thread safety, you’ll need
to allocate a new List every time, which would impair performance by object allocation.
This is the main reason why the collect method is useful for expressing reduction
working on a mutable container but crucially in a parallel-friendly way, as you’ll
learn later in the chapter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly