Java Gold ストリームAPI 例題集

コードの出力を答えてください

# 例題1

問1

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 5)
                           .map(n -> n * n)
                           .sum();
        System.out.println(sum);
    }
}
```

<details>
<summary>問1の解答</summary>
<pre class="console">
55
</pre>
</details>

# 例題2

問2

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
        String result = names.stream()
                             .filter(name -> name.startsWith("C"))
                             .findFirst()
                             .orElse("Not Found");
        System.out.println(result);
    }
}
```

<details>
<summary>問2の解答</summary>
<pre class="console">
Charlie
</pre>
</details>

# 例題3

問3

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        long count = Stream.of("apple", "banana", "cherry", "date")
                           .filter(fruit -> fruit.contains("a"))
                           .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問3の解答</summary>
<pre class="console">
3
</pre>
</details>

# 例題4

問4

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        boolean allMatch = IntStream.range(1, 10)
                                    .allMatch(n -> n < 10);
        System.out.println(allMatch);
    }
}
```

<details>
<summary>問4の解答</summary>
<pre class="console">
true
</pre>
</details>

# 例題5

問5

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 8, 5, 12);
        Optional<Integer> maxNumber = numbers.stream()
                                             .max(Integer::compare);
        System.out.println(maxNumber.orElse(-1));
    }
}
```

<details>
<summary>問5の解答</summary>
<pre class="console">
12
</pre>
</details>



# 例題6

問6

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("stream", "optional", "lambda", "java");
        String result = words.stream()
                             .map(String::toUpperCase)
                             .reduce((s1, s2) -> s1 + "-" + s2)
                             .orElse("Empty");
        System.out.println(result);
    }
}
```

<details>
<summary>問6の解答</summary>
<pre class="console">
STREAM-OPTIONAL-LAMBDA-JAVA
</pre>
</details>

# 例題7

問7

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        double average = Stream.of(3, 5, 7, 9, 11)
                               .mapToInt(Integer::intValue)
                               .average()
                               .orElse(0.0);
        System.out.println(average);
    }
}
```

<details>
<summary>問7の解答</summary>
<pre class="console">
7.0
</pre>
</details>

# 例題8

問8

```java
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 3, 2, 5, 4);
        Set<Integer> uniqueNumbers = numbers.stream()
                                            .collect(Collectors.toSet());
        System.out.println(uniqueNumbers);
    }
}
```

<details>
<summary>問8の解答</summary>
<pre class="console">
[2, 3, 4, 5]
</pre>
</details>

# 例題9

問9

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        int product = IntStream.rangeClosed(1, 5)
                               .reduce(1, (a, b) -> a * b);
        System.out.println(product);
    }
}
```

<details>
<summary>問9の解答</summary>
<pre class="console">
120
</pre>
</details>

# 例題10

問10

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("car", "bus", "bike", "truck");
        Optional<String> anyItem = items.stream()
                                        .filter(item -> item.length() > 3)
                                        .findAny();
        System.out.println(anyItem.orElse("No Match"));
    }
}
```

<details>
<summary>問10の解答</summary>
<pre class="console">
truck
</pre>
</details>


# 例題11

問11

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "apricot", "avocado");
        List<String> filteredWords = words.stream()
                                          .filter(word -> word.startsWith("a"))
                                          .collect(Collectors.toList());
        System.out.println(filteredWords);
    }
}
```

<details>
<summary>問11の解答</summary>
<pre class="console">
[apple, apricot, avocado]
</pre>
</details>

# 例題12

問12

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int result = numbers.stream()
                            .map(n -> n * 2)
                            .reduce(0, Integer::sum);
        System.out.println(result);
    }
}
```

<details>
<summary>問12の解答</summary>
<pre class="console">
30
</pre>
</details>

# 例題13

問13

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        String result = Stream.of("java", "stream", "api")
                              .sorted()
                              .collect(Collectors.joining(", "));
        System.out.println(result);
    }
}
```

<details>
<summary>問13の解答</summary>
<pre class="console">
api, java, stream
</pre>
</details>

# 例題14

問14

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
        boolean anyMatch = letters.stream()
                                  .anyMatch(letter -> letter.equals("c"));
        System.out.println(anyMatch);
    }
}
```

<details>
<summary>問14の解答</summary>
<pre class="console">
true
</pre>
</details>

# 例題15

問15

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "date");
        String longestWord = words.stream()
                                  .max((w1, w2) -> w1.length() - w2.length())
                                  .orElse("None");
        System.out.println(longestWord);
    }
}
```

<details>
<summary>問15の解答</summary>
<pre class="console">
banana
</pre>
</details>



# 例題16

問16

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 30, 40);
        Optional<Integer> first = numbers.stream()
                                         .filter(n -> n > 25)
                                         .findFirst();
        System.out.println(first.orElse(-1));
    }
}
```

<details>
<summary>問16の解答</summary>
<pre class="console">
30
</pre>
</details>

# 例題17

問17

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("red", "green", "blue", "yellow");
        List<Integer> lengths = words.stream()
                                     .map(String::length)
                                     .collect(Collectors.toList());
        System.out.println(lengths);
    }
}
```

<details>
<summary>問17の解答</summary>
<pre class="console">
[3, 5, 4, 6]
</pre>
</details>

# 例題18

問18

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        String[] result = Stream.of("dog", "cat", "elephant")
                                .filter(animal -> animal.length() > 3)
                                .toArray(String[]::new);
        System.out.println(String.join(", ", result));
    }
}
```

<details>
<summary>問18の解答</summary>
<pre class="console">
elephant
</pre>
</details>

# 例題19

問19

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        long count = IntStream.rangeClosed(1, 20)
                              .filter(n -> n % 2 == 0)
                              .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問19の解答</summary>
<pre class="console">
10
</pre>
</details>

# 例題20

問20

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("java", "optional", "stream", "lambda");
        Optional<String> longest = words.stream()
                                        .reduce((w1, w2) -> w1.length() > w2.length() ? w1 : w2);
        System.out.println(longest.orElse("None"));
    }
}
```

<details>
<summary>問20の解答</summary>
<pre class="console">
optional
</pre>
</details>



# 例題21

問21

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
        String result = items.stream()
                             .map(String::toUpperCase)
                             .collect(Collectors.joining(", "));
        System.out.println(result);
    }
}
```

<details>
<summary>問21の解答</summary>
<pre class="console">
APPLE, BANANA, CHERRY, DATE, ELDERBERRY
</pre>
</details>

# 例題22

問22

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> squared = numbers.stream()
                                       .map(n -> n * n)
                                       .collect(Collectors.toList());
        System.out.println(squared);
    }
}
```

<details>
<summary>問22の解答</summary>
<pre class="console">
[1, 4, 9, 16, 25]
</pre>
</details>

# 例題23

問23

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        long count = Stream.of("apple", "banana", "cherry", "date")
                           .flatMap(fruit -> Stream.of(fruit.split("")))
                           .distinct()
                           .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問23の解答</summary>
<pre class="console">
14
</pre>
</details>

# 例題24

問24

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 10)
                           .filter(n -> n % 2 == 0)
                           .sum();
        System.out.println(sum);
    }
}
```

<details>
<summary>問24の解答</summary>
<pre class="console">
30
</pre>
</details>

# 例題25

問25

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 10, 15, 20, 25);
        int result = numbers.stream()
                            .map(n -> n - 2)
                            .reduce(0, Integer::sum);
        System.out.println(result);
    }
}
```

<details>
<summary>問25の解答</summary>
<pre class="console">
65
</pre>
</details>


# 例題26

問26

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("apple", "banana", "cherry");
        String result = items.stream()
                             .filter(s -> s.startsWith("b"))
                             .findFirst()
                             .orElse("Not found");
        System.out.println(result);
    }
}
```

<details>
<summary>問26の解答</summary>
<pre class="console">
banana
</pre>
</details>

# 例題27

問27

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        long count = Stream.of("apple", "orange", "grape", "mango")
                           .filter(s -> s.contains("a"))
                           .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問27の解答</summary>
<pre class="console">
3
</pre>
</details>

# 例題28

問28

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 6, 9, 12);
        int sum = numbers.stream()
                         .mapToInt(Integer::intValue)
                         .sum();
        System.out.println(sum);
    }
}
```

<details>
<summary>問28の解答</summary>
<pre class="console">
30
</pre>
</details>

# 例題29

問29

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 6, 9, 12);
        Optional<Integer> max = numbers.stream()
                                       .max(Integer::compare);
        System.out.println(max.orElse(0));
    }
}
```

<details>
<summary>問29の解答</summary>
<pre class="console">
12
</pre>
</details>

# 例題30

問30

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("java", "stream", "api", "lambda");
        String joined = words.stream()
                             .sorted()
                             .collect(Collectors.joining("-"));
        System.out.println(joined);
    }
}
```

<details>
<summary>問30の解答</summary>
<pre class="console">
api-java-lambda-stream
</pre>
</details>

# 例題31

問31

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        boolean allMatch = Stream.of(2, 4, 6, 8)
                                 .allMatch(n -> n % 2 == 0);
        System.out.println(allMatch);
    }
}
```

<details>
<summary>問31の解答</summary>
<pre class="console">
true
</pre>
</details>

# 例題32

問32

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 30, 40);
        int min = numbers.stream()
                         .min(Integer::compare)
                         .orElse(0);
        System.out.println(min);
    }
}
```

<details>
<summary>問32の解答</summary>
<pre class="console">
10
</pre>
</details>

# 例題33

問33

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        long count = IntStream.range(1, 100)
                              .filter(n -> n % 3 == 0)
                              .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問33の解答</summary>
<pre class="console">
33
</pre>
</details>

# 例題34

問34

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry");
        boolean noneMatch = words.stream()
                                 .noneMatch(word -> word.length() > 6);
        System.out.println(noneMatch);
    }
}
```

<details>
<summary>問34の解答</summary>
<pre class="console">
true
</pre>
</details>

# 例題35

問35

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("cat", "dog", "elephant");
        Stream<String> distinctStream = stream.map(String::toUpperCase)
                                              .distinct();
        distinctStream.forEach(System.out::println);
    }
}
```

<details>
<summary>問35の解答</summary>
<pre class="console">
CAT
DOG
ELEPHANT
</pre>
</details>

# 例題36

問36

```java
import java.util.stream.IntStream;

public class StreamExample {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 10)
                           .map(n -> n * n)
                           .sum();
        System.out.println(sum);
    }
}
```

<details>
<summary>問36の解答</summary>
<pre class="console">
385
</pre>
</details>

# 例題37

問37

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("apple", "orange", "banana");
        String result = fruits.stream()
                              .reduce((f1, f2) -> f1 + " & " + f2)
                              .orElse("None");
        System.out.println(result);
    }
}
```

<details>
<summary>問37の解答</summary>
<pre class="console">
apple & orange & banana
</pre>
</details>

# 例題38

問38

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int product = numbers.stream()
                             .reduce(1, (a, b) -> a * b);
        System.out.println(product);
    }
}
```

<details>
<summary>問38の解答</summary>
<pre class="console">
120
</pre>
</details>

# 例題39

問39

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.iterate(1, n -> n + 1)
                                       .limit(5);
        int sum = stream.reduce(0, Integer::sum);
        System.out.println(sum);
    }
}
```

<details>
<summary>問39の解答</summary>
<pre class="console">
15
</pre>
</details>

# 例題40

問40

```java
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("stream", "optional", "lambda", "java", "stream");
        Set<String> uniqueWords = words.stream()
                                       .collect(Collectors.toSet());
        System.out.println(uniqueWords);
    }
}
```

<details>
<summary>問40の解答</summary>
<pre class="console">
[stream, optional, lambda, java]
</pre>
</details>




# 例題41

問41

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<String> strings = numbers.stream()
                                      .map(n -> "Number: " + n)
                                      .collect(Collectors.toList());
        System.out.println(strings);
    }
}
```

<details>
<summary>問41の解答</summary>
<pre class="console">
[Number: 1, Number: 2, Number: 3, Number: 4, Number: 5]
</pre>
</details>

# 例題42

問42

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("functional", "programming", "java");
        long count = words.stream()
                          .flatMap(word -> Arrays.stream(word.split("")))
                          .distinct()
                          .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問42の解答</summary>
<pre class="console">
13
</pre>
</details>

# 例題43

問43

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("apple", "banana", "cherry", "date");
        String result = fruits.stream()
                              .filter(fruit -> fruit.contains("e"))
                              .collect(Collectors.joining(", "));
        System.out.println(result);
    }
}
```

<details>
<summary>問43の解答</summary>
<pre class="console">
apple, cherry, date
</pre>
</details>

# 例題44

問44

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 30);
        Optional<Integer> first = numbers.stream()
                                         .findFirst();
        System.out.println(first.orElse(0));
    }
}
```

<details>
<summary>問44の解答</summary>
<pre class="console">
10
</pre>
</details>

# 例題45

問45

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("A", "B", "C", "D");
        List<String> lowerCaseItems = items.stream()
                                           .map(String::toLowerCase)
                                           .collect(Collectors.toList());
        System.out.println(lowerCaseItems);
    }
}
```

<details>
<summary>問45の解答</summary>
<pre class="console">
[a, b, c, d]
</pre>
</details>

# 例題46

問46

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 10, 15, 20);
        boolean anyMatch = numbers.stream()
                                  .anyMatch(n -> n % 3 == 0);
        System.out.println(anyMatch);
    }
}
```

<details>
<summary>問46の解答</summary>
<pre class="console">
true
</pre>
</details>

# 例題47

問47

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("optional", "stream", "lambda");
        Optional<String> any = words.stream()
                                    .findAny();
        System.out.println(any.orElse("Not found"));
    }
}
```

<details>
<summary>問47の解答</summary>
<pre class="console">
optional
</pre>
</details>

# 例題48

問48

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
        boolean noneMatch = numbers.stream()
                                   .noneMatch(n -> n % 3 == 0);
        System.out.println(noneMatch);
    }
}
```

<details>
<summary>問48の解答</summary>
<pre class="console">
false
</pre>
</details>

# 例題49

問49

```java
import java.util.stream.Stream;

public class StreamExample {
    public static void main(String[] args) {
        long count = Stream.of("java", "stream", "api", "lambda")
                           .flatMap(s -> Stream.of(s.split("")))
                           .filter(ch -> ch.equals("a"))
                           .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問49の解答</summary>
<pre class="console">
6
</pre>
</details>

# 例題50

問50

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        long evenCount = numbers.stream()
                                .filter(n -> n % 2 == 0)
                                .count();
        System.out.println(evenCount);
    }
}
```

<details>
<summary>問50の解答</summary>
<pre class="console">
3
</pre>
</details>

# 例題51

問51

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 7, 2, 5, 8);
        Optional<Integer> min = numbers.stream()
                                       .min(Integer::compareTo);
        System.out.println(min.orElse(0));
    }
}
```

<details>
<summary>問51の解答</summary>
<pre class="console">
2
</pre>
</details>

# 例題52

問52

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 30, 40);
        int product = numbers.stream()
                             .reduce(1, (a, b) -> a * b);
        System.out.println(product);
    }
}
```

<details>
<summary>問52の解答</summary>
<pre class="console">
240000
</pre>
</details>

# 例題53

問53

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("hello", "world", "java");
        Optional<String> longest = words.stream()
                                        .max((s1, s2) -> s1.length() - s2.length());
        System.out.println(longest.orElse("No words"));
    }
}
```

<details>
<summary>問53の解答</summary>
<pre class="console">
hello
</pre>
比較した結果0の場合、最初の要素が選ばれる。
文字列の長さが同じ時、辞書順に最後を選びたい時の例

```java
Optional<String> longest = words.stream()
                                .max(Comparator.comparingInt(String::length)
                                               .thenComparing(Comparator.naturalOrder()));
System.out.println(longest.);
```
出力
```
world
```
[`conparingInt`](https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Comparator.html#comparingInt(java.util.function.ToIntFunction))

[`thenComparing`](https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Comparator.html#thenComparing(java.util.Comparator))
</details>


# 例題54

問54

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("Java", "Stream", "API");
        List<String> lowerCaseItems = items.stream()
                                           .map(String::toLowerCase)
                                           .collect(Collectors.toList());
        System.out.println(lowerCaseItems);
    }
}
```

<details>
<summary>問54の解答</summary>
<pre class="console">
[java, stream, api]
</pre>
</details>

# 例題55

問55

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
        int sum = numbers.stream()
                         .filter(n -> n > 5)
                         .mapToInt(Integer::intValue)
                         .sum();
        System.out.println(sum);
    }
}
```

<details>
<summary>問55の解答</summary>
<pre class="console">
24
</pre>

[`intValue`](https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/lang/Integer.html#intValue())
</details>

# 例題56

問56

```java
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("hello", "world", "java", "stream");
        Set<Character> characters = words.stream()
                                         .flatMap(word -> word.chars().mapToObj(c -> (char) c))
                                         .collect(Collectors.toCollection(TreeSet::new));
        System.out.println(characters);
    }
}
```

<details>
<summary>問56の解答</summary>
<pre class="console">
[a, d, e, h, j, l, m, o, r, s, t, v, w]
</pre>

[`chars`](https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/lang/String.html#chars())

`TreeSet::new`から`LinkedHashSet::new`に変えた場合
```
[h, e, l, o, w, r, d, j, a, v, s, t, m]
```
</details>

# 例題57

問57

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 15, 20, 25, 30);
        List<Integer> squared = numbers.stream()
                                       .map(n -> n * n)
                                       .collect(Collectors.toList());
        System.out.println(squared);
    }
}
```

<details>
<summary>問57の解答</summary>
<pre class="console">
[100, 225, 400, 625, 900]
</pre>
</details>

# 例題58

問58

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 10, 15, 20, 25);
        OptionalDouble average = numbers.stream()
                                        .mapToInt(Integer::intValue)
                                        .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問58の解答</summary>
<pre class="console">
15.0
</pre>
</details>

# 例題59

問59

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("java", "stream", "api", "lambda");
        boolean allMatch = words.stream()
                                .allMatch(word -> word.length() > 2);
        System.out.println(allMatch);
    }
}
```

<details>
<summary>問59の解答</summary>
<pre class="console">
true
</pre>
</details>

# 例題60

問60

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 3, 5, 7, 11, 13);
        long primeCount = numbers.stream()
                                 .filter(StreamExample::isPrime)
                                 .count();
        System.out.println(primeCount);
    }

    private static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) return false;
        }
        return true;
    }
}
```

<details>
<summary>問60の解答</summary>
<pre class="console">
6
</pre>
</details>



# 例題61

問61

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50);
        OptionalDouble average = numbers.stream()
                                        .mapToInt(Integer::intValue)
                                        .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問61の解答</summary>
<pre class="console">
30.0
</pre>
</details>

# 例題62

問62

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        OptionalInt max = numbers.stream()
                                 .mapToInt(Integer::intValue)
                                 .max();
        System.out.println(max.orElse(0));
    }
}
```

<details>
<summary>問62の解答</summary>
<pre class="console">
5
</pre>
</details>

# 例題63

問63

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50);
        OptionalInt min = numbers.stream()
                                 .mapToInt(Integer::intValue)
                                 .min();
        System.out.println(min.orElse(0));
    }
}
```

<details>
<summary>問63の解答</summary>
<pre class="console">
10
</pre>
</details>

# 例題64

問64

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int sum = numbers.stream()
                         .mapToInt(Integer::intValue)
                         .sum();
        System.out.println(sum);
    }
}
```

<details>
<summary>問64の解答</summary>
<pre class="console">
15
</pre>
</details>

# 例題65

問65

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "apple", "orange", "banana");
        Map<String, Long> wordCount = words.stream()
                                           .collect(Collectors.groupingBy(word -> word, Collectors.counting()));
        System.out.println(wordCount);
    }
}
```

<details>
<summary>問65の解答</summary>
<pre class="console">
{orange=1, banana=2, apple=2}
</pre>
</details>

# 例題66

問66

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> values = Arrays.asList(2.5, 3.5, 4.5, 5.5);
        double sum = values.stream()
                           .collect(Collectors.summingDouble(Double::doubleValue));
        System.out.println(sum);
    }
}
```

<details>
<summary>問66の解答</summary>
<pre class="console">
16.0
</pre>
</details>

# 例題67

問67

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Map<Boolean, List<Integer>> partitioned = numbers.stream()
                                                         .collect(Collectors.partitioningBy(n -> n % 2 == 0));
        System.out.println(partitioned);
    }
}
```

<details>
<summary>問67の解答</summary>
<pre class="console">
{false=[1, 3, 5], true=[2, 4]}
</pre>
</details>

# 例題68

問68

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("java", "stream", "api", "lambda");
        Map<Integer, List<String>> groupedByLength = words.stream()
                                                          .collect(Collectors.groupingBy(String::length));
        System.out.println(groupedByLength);
    }
}
```

<details>
<summary>問68の解答</summary>
<pre class="console">
{3=[api], 4=[java, lambda], 6=[stream]}
</pre>
</details>

# 例題69

問69

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 10, 15, 20);
        OptionalDouble average = numbers.stream()
                                        .mapToInt(Integer::intValue)
                                        .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問69の解答</summary>
<pre class="console">
12.5
</pre>
</details>

# 例題70

問70

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> values = Arrays.asList(1.0, 2.0, 3.0, 4.0);
        double sum = values.stream()
                           .collect(Collectors.summingDouble(Double::doubleValue));
        System.out.println(sum);
    }
}
```

<details>
<summary>問70の解答</summary>
<pre class="console">
10.0
</pre>
</details>

# 例題71

問71

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(4, 8, 12, 16);
        OptionalInt max = numbers.stream()
                                 .mapToInt(Integer::intValue)
                                 .max();
        System.out.println(max.orElse(0));
    }
}
```

<details>
<summary>問71の解答</summary>
<pre class="console">
16
</pre>
</details>

# 例題72

問72

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "apple", "orange", "banana");
        Map<String, Double> averageLength = words.stream()
                                                 .collect(Collectors.groupingBy(String::toString, Collectors.averagingInt(String::length)));
        System.out.println(averageLength);
    }
}
```

<details>
<summary>問72の解答</summary>
<pre class="console">
{orange=6.0, banana=6.0, apple=5.0}
</pre>
</details>

# 例題73

問73

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> prices = Arrays.asList(99.99, 149.99, 199.99);
        double sum = prices.stream()
                           .collect(Collectors.summingDouble(Double::doubleValue));
        System.out.println(sum);
    }
}
```

<details>
<summary>問73の解答</summary>
<pre class="console">
449.97
</pre>
</details>

# 例題74

問74

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("one", "two", "three", "four");
        Map<Integer, Long> lengthCount = words.stream()
                                              .collect(Collectors.groupingBy(String::length, Collectors.counting()));
        System.out.println(lengthCount);
    }
}
```

<details>
<summary>問74の解答</summary>
<pre class="console">
{3=2, 4=2, 5=1}
</pre>
</details>

# 例題75

問75

```java
import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
        long count = numbers.stream()
                            .count();
        System.out.println(count);
    }
}
```

<details>
<summary>問75の解答</summary>
<pre class="console">
5
</pre>
</details>

# 例題76

問76

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "apple", "banana", "cherry");
        Map<String, Long> frequency = words.stream()
                                           .collect(Collectors.groupingBy(String::toString, Collectors.counting()));
        System.out.println(frequency);
    }
}
```

<details>
<summary>問76の解答</summary>
<pre class="console">
{banana=2, cherry=1, apple=2}
</pre>
</details>

# 例題77

問77

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 15, 25, 35);
        OptionalDouble average = numbers.stream()
                                        .mapToInt(Integer::intValue)
                                        .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問77の解答</summary>
<pre class="console">
20.0
</pre>
</details>

# 例題78

問78

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Map<Boolean, List<Integer>> partitioned = numbers.stream()
                                                         .collect(Collectors.partitioningBy(n -> n % 2 == 0));
        System.out.println(partitioned);
    }
}
```

<details>
<summary>問78の解答</summary>
<pre class="console">
{false=[1, 3, 5], true=[2, 4]}
</pre>
</details>

# 例題79

問79

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> values = Arrays.asList(1.5, 2.5, 3.5);
        OptionalDouble average = values.stream()
                                        .mapToDouble(Double::doubleValue)
                                        .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問79の解答</summary>
<pre class="console">
2.5
</pre>
</details>

# 例題80

問80

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> values = Arrays.asList(10.0, 20.0, 30.0);
        Map<Boolean, Double> partitionedSum = 
                    values.stream()
                          .collect(Collectors.partitioningBy(
                             v -> v > 15.0, Collectors.summingDouble(Double::doubleValue)));
                             
        System.out.println(partitionedSum);
    }
}
```

<details>
<summary>問80の解答</summary>
<pre class="console">
{false=10.0, true=50.0}
</pre>
</details>

# 例題81

問81

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "date");
        Optional<String> longestWord = words.stream()
                                            .collect(Collectors.maxBy((a, b) -> Integer.compare(a.length(), b.length())));
        System.out.println(longestWord.orElse("No words"));
    }
}
```

<details>
<summary>問81の解答</summary>
<pre class="console">
banana
</pre>
</details>

# 例題82

問82

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "date");
        Optional<String> shortestWord = words.stream()
                                             .collect(Collectors.minBy((a, b) -> Integer.compare(a.length(), b.length())));
        System.out.println(shortestWord.orElse("No words"));
    }
}
```

<details>
<summary>問82の解答</summary>
<pre class="console">
date
</pre>
</details>

# 例題83

問83

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Map<Boolean, List<Integer>> partitioned = numbers.stream()
                                                         .collect(Collectors.partitioningBy(n -> n % 2 == 0));
        System.out.println(partitioned);
    }
}
```

<details>
<summary>問83の解答</summary>
<pre class="console">
{false=[1, 3, 5], true=[2, 4]}
</pre>
</details>

# 例題84

問84

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
        OptionalInt max = numbers.stream()
                                 .mapToInt(Integer::intValue)
                                 .max();
        System.out.println(max.orElse(0));
    }
}
```

<details>
<summary>問84の解答</summary>
<pre class="console">
10
</pre>
</details>

# 例題85

問85

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        OptionalInt min = numbers.stream()
                                 .mapToInt(Integer::intValue)
                                 .min();
        System.out.println(min.orElse(0));
    }
}
```

<details>
<summary>問85の解答</summary>
<pre class="console">
1
</pre>
</details>

# 例題86

問86

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<String> mapped = numbers.stream()
                                     .map(String::valueOf)
                                     .collect(Collectors.toList());
        System.out.println(mapped);
    }
}
```

<details>
<summary>問86の解答</summary>
<pre class="console">
[1, 2, 3, 4, 5]
</pre>
</details>

# 例題87

問87

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("java", "stream", "api", "lambda");
        Map<Integer, List<String>> groupedByLength = words.stream()
                                                          .collect(Collectors.groupingBy(String::length));
        System.out.println(groupedByLength);
    }
}
```

<details>
<summary>問87の解答</summary>
<pre class="console">
{3=[api], 4=[java], 6=[stream, lambda]}
</pre>
</details>

# 例題88

問88

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> values = Arrays.asList(2.0, 4.0, 6.0);
        OptionalDouble average = values.stream()
                                       .mapToDouble(Double::doubleValue)
                                       .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問88の解答</summary>
<pre class="console">
4.0
</pre>
</details>

# 例題89

問89

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("one", "two", "three");
        Map<Integer, String> longestWordByLength = words.stream()
                                                        .collect(Collectors.groupingBy(String::length,
                                                                                       Collectors.collectingAndThen(
                                                                                           Collectors.maxBy((a, b) -> Integer.compare(a.length(), b.length())),
                                                                                           opt -> opt.orElse("None"))));
        System.out.println(longestWordByLength);
    }
}
```

<details>
<summary>問89の解答</summary>
<pre class="console">
{3=one, 5=three}
</pre>

`collect(Collectors.groupingBy)`

文字列の長さ (String::length) に基づいて、ストリームの要素をグループ化します。
Map<Integer, List<String>> のように、キーが文字列の長さ、値がその長さを持つ文字列のリストとなります。

`Collectors.collectingAndThen`

各グループ（各長さに対応するリスト）に対して、さらに操作を行います。ここでは、グループ内で最も長い単語を取得するために maxBy を使用します。

`Collectors.maxBy((a, b) -> Integer.compare(a.length(), b.length()))`

グループ内の文字列を比較し、最も長い単語を取得します。長さが同じ場合、最初に出現する文字列が選ばれます。Optional<String> が返されます。

`opt -> opt.orElse("None")`

Optional<String> から値を取り出します。もし Optional が空の場合は "None" を返します。

</details>

# 例題90

問90

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(7, 8, 10, 12);
        Optional<Integer> min = numbers.stream()
                                        .collect(Collectors.minBy(Integer::compareTo));
        System.out.println(min.orElse(0));
    }
}
```

<details>
<summary>問90の解答</summary>
<pre class="console">
7
</pre>
</details>



# 例題90

問90

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10, 12, 14);
        Optional<Integer> maxOdd = numbers.stream()
                                          .filter(n -> n % 2 != 0)
                                          .collect(Collectors.maxBy(Integer::compareTo));
        System.out.println(maxOdd.orElse(-1));
    }
}
```

<details>
<summary>問90の解答</summary>
<pre class="console">
-1
</pre>
このコードは、リスト内の奇数の最大値を求めます。しかし、リスト内には奇数が存在しないため、`Optional.empty()`が返され、`orElse(-1)`により`-1`が出力されます。
</details>
 
# 例題91

問91

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("a", "ab", "abc", "abcd");
        Map<Integer, String> result = words.stream()
                                           .collect(Collectors.toMap(
                                               String::length,
                                               Function.identity(),
                                               (existing, replacement) -> existing + ", " + replacement));
        System.out.println(result);
    }
}
```

<details>
<summary>問91の解答</summary>
<pre class="console">{1=a, 2=ab, 3=abc, 4=abcd}</pre>

文字列の長さでグループ化し、長さごとに文字列を連結していきます。長さごとに最初に出現した文字列だけが選ばれるため、出力は長さごとに単一の文字列が表示されます。

`collect(Collectors.toMap(...))`

ストリームの要素を Map<Integer, String> に収集します。toMap メソッドは、指定されたキーと値のペアを Map に追加します。

`String::length, Function.identity()`

String::length: 各文字列の長さ（Integer）をキーとして使用します。
Function.identity(): 各文字列自体（String）を値として使用します。

`(existing, replacement) -> existing + ", " + replacement`

同じキー（文字列の長さ）が複数回登場した場合、existing（既存の値）と replacement（新しい値）をカンマで結合します。
例えば、長さが2の文字列 "ab" と "ab" がある場合、これらは "ab, ab" という形で結合されます。

例
```java
List<String> words = Arrays.asList("ab", "cd", "ef", "gh");
Map<Integer, String> result = words.stream()
    .collect(Collectors.toMap(
        String::length,
        Function.identity(),
        (existing, replacement) -> existing + ", " + replacement));
System.out.println(result);
```

```
{2=ab, cd, ef, gh}
```

</details>



# 例題92

問92


```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "apple", "cherry");
        Map<Character, Long> countByInitial = words.stream()
                                                  .collect(Collectors.groupingBy(
                                                      word -> word.charAt(0),
                                                      Collectors.counting()));
        System.out.println(countByInitial);
    }
}
```

<details>
<summary>問92の解答</summary>
<pre class="console">{a=2, b=1, c=2}</pre>

各単語の最初の文字でグループ化し、その文字で始まる単語の数をカウントします。
「apple」と「banana」がそれぞれ2回と1回出現し、「cherry」が2回出現します。
</details>


# 例題93

問93

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Double> values = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
        OptionalDouble average = values.stream()
                                       .mapToDouble(d -> d * d)
                                       .average();
        System.out.println(average.orElse(0.0));
    }
}
```

<details>
<summary>問93の解答</summary>
<pre class="console">
11.0
</pre>

</details>



# 例題94

問94

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("java", "python", "java", "c++", "python");
		Map<String, Optional<String>> maxLengthByWord = words.stream()
		    .collect(Collectors.groupingBy(
		        Function.identity(),
		        Collectors.reducing((w1, w2) -> w1.length() >= w2.length() ? w1 : w2)));
		System.out.println(maxLengthByWord);
    }
}
```

<details>
<summary>問94の解答</summary>
<pre class="console">
{c++=Optional[c++], python=Optional[python], java=Optional[java]}
</pre>
このコードは、単語ごとに最大長の単語を求めますが、すべての単語の長さは同じなので、それぞれの単語が自分自身が最大となります。
</details>




# 例題95

問95

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("abc", "abcd", "ab", "a", "c");
        Map<Integer, String> concatenatedByLength = words.stream()
                                                         .collect(Collectors.groupingBy(
                                                             String::length,
                                                             Collectors.mapping(
                                                                 Function.identity(),
                                                                 Collectors.joining("+"))));
        System.out.println(concatenatedByLength);
    }
}
```

<details>
<summary>問95の解答</summary>
<pre class="console">
{1=a+c, 2=ab, 3=abc, 4=abcd}
</pre>
文字列を長さでグループ化し、長さごとに文字列を連結します。出力は各長さごとに連結された文字列になります。
</details>




# 例題96

問96

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        Optional<Integer> minOdd = numbers.stream()
                                           .filter(n -> n % 2 != 0)
                                           .collect(Collectors.minBy(Integer::compareTo));
        System.out.println(minOdd.orElse(-1));
    }
}
```

<details>
<summary>問96の解答</summary>
<pre class="console">
1
</pre>
このコードは、リスト内の奇数の最小値を求めます。リストには1が含まれているため、最小値として1が出力されます。
</details>


# 例題97

問97

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("cat", "dog", "elephant", "bear");
        Map<Character, Double> averageLengthByInitial = words.stream()
                                                          .collect(Collectors.groupingBy(
                                                              word -> word.charAt(0),
                                                              Collectors.averagingDouble(String::length)));
        System.out.println(averageLengthByInitial);
    }
}
```

<details>
<summary>問97の解答</summary>
<pre class="console">
{b=4.0, c=3.0, d=3.0, e=8.0}
</pre>
文字列の最初の文字でグループ化し、各グループの文字列の長さの平均を求めます。グループごとの平均長さが出力されます。
</details>
  


# 例題98

問98

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
        Map<Boolean, Integer> partitionedSum = numbers.stream()
                                                      .collect(Collectors.partitioningBy(
                                                          n -> n % 2 == 0,
                                                          Collectors.summingInt(Integer::intValue)));
        System.out.println(partitionedSum);
    }
}
```

<details>
<summary>問98の解答</summary>
<pre class="console">
{false=0, true=30}
</pre>
</details>



# 例題99

問99

```java
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class StreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        OptionalDouble averageEven = numbers.stream()
                                             .filter(n -> n % 2 == 0)
                                             .mapToInt(Integer::intValue)
                                             .average();
        System.out.println(averageEven.orElse(-1));
    }
}
```

<details>
<summary>問99の解答</summary>
<pre class="console">
4.0
</pre>

</details>



# 例題100

問100

```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "kiwi", "banana", "apple", "kiwi", "kiwi");
        Map<String, Long> frequency = words.stream()
                                           .collect(Collectors.groupingBy(
                                               Function.identity(),
                                               Collectors.counting()));
        System.out.println(frequency);
    }
}
```

<details>
<summary>問100の解答</summary>
<pre class="console">
{banana=2, apple=2, kiwi=3}
</pre>
このコードは、リスト内の各単語の出現頻度をカウントします。
出現頻度が2回の「apple」と「banana」、3回の「kiwi」が出力されます。
</details>


https://qiita.com/T-ngtj/private/df254aacd05be698199a