コードの出力を答えてください
例題1
問1
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);
}
}
問1の解答
55
例題2
問2
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);
}
}
問2の解答
Charlie
例題3
問3
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);
}
}
問3の解答
3
例題4
問4
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);
}
}
問4の解答
true
例題5
問5
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));
}
}
問5の解答
12
例題6
問6
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);
}
}
問6の解答
STREAM-OPTIONAL-LAMBDA-JAVA
例題7
問7
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);
}
}
問7の解答
7.0
例題8
問8
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);
}
}
問8の解答
[2, 3, 4, 5]
例題9
問9
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);
}
}
問9の解答
120
例題10
問10
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"));
}
}
問10の解答
truck
例題11
問11
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);
}
}
問11の解答
[apple, apricot, avocado]
例題12
問12
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);
}
}
問12の解答
30
例題13
問13
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);
}
}
問13の解答
api, java, stream
例題14
問14
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);
}
}
問14の解答
true
例題15
問15
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);
}
}
問15の解答
banana
例題16
問16
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));
}
}
問16の解答
30
例題17
問17
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);
}
}
問17の解答
[3, 5, 4, 6]
例題18
問18
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));
}
}
問18の解答
elephant
例題19
問19
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);
}
}
問19の解答
10
例題20
問20
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"));
}
}
問20の解答
optional
例題21
問21
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);
}
}
問21の解答
APPLE, BANANA, CHERRY, DATE, ELDERBERRY
例題22
問22
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);
}
}
問22の解答
[1, 4, 9, 16, 25]
例題23
問23
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);
}
}
問23の解答
14
例題24
問24
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);
}
}
問24の解答
30
例題25
問25
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);
}
}
問25の解答
65
例題26
問26
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);
}
}
問26の解答
banana
例題27
問27
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);
}
}
問27の解答
3
例題28
問28
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);
}
}
問28の解答
30
例題29
問29
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));
}
}
問29の解答
12
例題30
問30
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);
}
}
問30の解答
api-java-lambda-stream
例題31
問31
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);
}
}
問31の解答
true
例題32
問32
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);
}
}
問32の解答
10
例題33
問33
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);
}
}
問33の解答
33
例題34
問34
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);
}
}
問34の解答
true
例題35
問35
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);
}
}
問35の解答
CAT
DOG
ELEPHANT
例題36
問36
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);
}
}
問36の解答
385
例題37
問37
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);
}
}
問37の解答
apple & orange & banana
例題38
問38
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);
}
}
問38の解答
120
例題39
問39
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);
}
}
問39の解答
15
例題40
問40
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);
}
}
問40の解答
[stream, optional, lambda, java]
例題41
問41
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);
}
}
問41の解答
[Number: 1, Number: 2, Number: 3, Number: 4, Number: 5]
例題42
問42
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);
}
}
問42の解答
13
例題43
問43
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);
}
}
問43の解答
apple, cherry, date
例題44
問44
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));
}
}
問44の解答
10
例題45
問45
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);
}
}
問45の解答
[a, b, c, d]
例題46
問46
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);
}
}
問46の解答
true
例題47
問47
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"));
}
}
問47の解答
optional
例題48
問48
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);
}
}
問48の解答
false
例題49
問49
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);
}
}
問49の解答
6
例題50
問50
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);
}
}
問50の解答
3
例題51
問51
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));
}
}
問51の解答
2
例題52
問52
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);
}
}
問52の解答
240000
例題53
問53
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"));
}
}
問53の解答
hello
Optional<String> longest = words.stream()
.max(Comparator.comparingInt(String::length)
.thenComparing(Comparator.naturalOrder()));
System.out.println(longest.);
出力
world
例題54
問54
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);
}
}
問54の解答
[java, stream, api]
例題55
問55
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);
}
}
例題56
問56
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);
}
}
問56の解答
[a, d, e, h, j, l, m, o, r, s, t, v, w]
TreeSet::new
からLinkedHashSet::new
に変えた場合
[h, e, l, o, w, r, d, j, a, v, s, t, m]
例題57
問57
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);
}
}
問57の解答
[100, 225, 400, 625, 900]
例題58
問58
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));
}
}
問58の解答
15.0
例題59
問59
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);
}
}
問59の解答
true
例題60
問60
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;
}
}
問60の解答
6
例題61
問61
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));
}
}
問61の解答
30.0
例題62
問62
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));
}
}
問62の解答
5
例題63
問63
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));
}
}
問63の解答
10
例題64
問64
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);
}
}
問64の解答
15
例題65
問65
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);
}
}
問65の解答
{orange=1, banana=2, apple=2}
例題66
問66
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);
}
}
問66の解答
16.0
例題67
問67
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);
}
}
問67の解答
{false=[1, 3, 5], true=[2, 4]}
例題68
問68
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);
}
}
問68の解答
{3=[api], 4=[java, lambda], 6=[stream]}
例題69
問69
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));
}
}
問69の解答
12.5
例題70
問70
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);
}
}
問70の解答
10.0
例題71
問71
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));
}
}
問71の解答
16
例題72
問72
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);
}
}
問72の解答
{orange=6.0, banana=6.0, apple=5.0}
例題73
問73
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);
}
}
問73の解答
449.97
例題74
問74
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);
}
}
問74の解答
{3=2, 4=2, 5=1}
例題75
問75
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);
}
}
問75の解答
5
例題76
問76
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);
}
}
問76の解答
{banana=2, cherry=1, apple=2}
例題77
問77
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));
}
}
問77の解答
20.0
例題78
問78
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);
}
}
問78の解答
{false=[1, 3, 5], true=[2, 4]}
例題79
問79
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));
}
}
問79の解答
2.5
例題80
問80
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);
}
}
問80の解答
{false=10.0, true=50.0}
例題81
問81
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"));
}
}
問81の解答
banana
例題82
問82
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"));
}
}
問82の解答
date
例題83
問83
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);
}
}
問83の解答
{false=[1, 3, 5], true=[2, 4]}
例題84
問84
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));
}
}
問84の解答
10
例題85
問85
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));
}
}
問85の解答
1
例題86
問86
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);
}
}
問86の解答
[1, 2, 3, 4, 5]
例題87
問87
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);
}
}
問87の解答
{3=[api], 4=[java], 6=[stream, lambda]}
例題88
問88
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));
}
}
問88の解答
4.0
例題89
問89
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);
}
}
問89の解答
{3=one, 5=three}
collect(Collectors.groupingBy)
文字列の長さ (String::length) に基づいて、ストリームの要素をグループ化します。
Map> のように、キーが文字列の長さ、値がその長さを持つ文字列のリストとなります。
Collectors.collectingAndThen
各グループ(各長さに対応するリスト)に対して、さらに操作を行います。ここでは、グループ内で最も長い単語を取得するために maxBy を使用します。
Collectors.maxBy((a, b) -> Integer.compare(a.length(), b.length()))
グループ内の文字列を比較し、最も長い単語を取得します。長さが同じ場合、最初に出現する文字列が選ばれます。Optional が返されます。
opt -> opt.orElse("None")
Optional から値を取り出します。もし Optional が空の場合は "None" を返します。
例題90
問90
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));
}
}
問90の解答
7
例題90
問90
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));
}
}
問90の解答
-1
例題91
問91
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);
}
}
問91の解答
{1=a, 2=ab, 3=abc, 4=abcd}
文字列の長さでグループ化し、長さごとに文字列を連結していきます。長さごとに最初に出現した文字列だけが選ばれるため、出力は長さごとに単一の文字列が表示されます。
collect(Collectors.toMap(...))
ストリームの要素を Map に収集します。toMap メソッドは、指定されたキーと値のペアを Map に追加します。
String::length, Function.identity()
String::length: 各文字列の長さ(Integer)をキーとして使用します。
Function.identity(): 各文字列自体(String)を値として使用します。
(existing, replacement) -> existing + ", " + replacement
同じキー(文字列の長さ)が複数回登場した場合、existing(既存の値)と replacement(新しい値)をカンマで結合します。
例えば、長さが2の文字列 "ab" と "ab" がある場合、これらは "ab, ab" という形で結合されます。
例
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}
例題92
問92
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);
}
}
問92の解答
{a=2, b=1, c=2}
各単語の最初の文字でグループ化し、その文字で始まる単語の数をカウントします。
「apple」と「banana」がそれぞれ2回と1回出現し、「cherry」が2回出現します。
例題93
問93
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));
}
}
問93の解答
11.0
例題94
問94
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);
}
}
問94の解答
{c++=Optional[c++], python=Optional[python], java=Optional[java]}
例題95
問95
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);
}
}
問95の解答
{1=a+c, 2=ab, 3=abc, 4=abcd}
例題96
問96
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));
}
}
問96の解答
1
例題97
問97
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);
}
}
問97の解答
{b=4.0, c=3.0, d=3.0, e=8.0}
例題98
問98
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);
}
}
問98の解答
{false=0, true=30}
例題99
問99
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));
}
}
問99の解答
4.0
例題100
問100
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);
}
}
問100の解答
{banana=2, apple=2, kiwi=3}