|
| 1 | +Java Collectors API & Stream Operations – Practice Qs: |
| 2 | + |
| 3 | +1. Collecting to a List |
| 4 | +----------------------- |
| 5 | +- What is the purpose of using Collectors.toList() in the following snippet? |
| 6 | + |
| 7 | + List<String> res = names.stream() |
| 8 | + .filter(name -> name.startsWith("A")) |
| 9 | + .collect(Collectors.toList()); |
| 10 | + |
| 11 | +- How does filtering and then collecting into a list work in this context? |
| 12 | + |
| 13 | + |
| 14 | +2. Collecting to a Set |
| 15 | +---------------------- |
| 16 | +- How does Collectors.toSet() differ from toList() in terms of handling duplicates and ordering? |
| 17 | + |
| 18 | + Set<Integer> set = nums.stream().collect(Collectors.toSet()); |
| 19 | + |
| 20 | + |
| 21 | +3. Collecting to a Specific Collection |
| 22 | +-------------------------------------- |
| 23 | +- Explain the use of Collectors.toCollection(() -> new ArrayDeque<>()). |
| 24 | +- In what scenarios might you choose to collect stream elements into an ArrayDeque instead of a List or Set? |
| 25 | + |
| 26 | + |
| 27 | +4. Joining Strings |
| 28 | +------------------ |
| 29 | +- How does the Collectors.joining(", ") method operate in this example? |
| 30 | + |
| 31 | + String concatenatedNames = names.stream() |
| 32 | + .map(String::toUpperCase) |
| 33 | + .collect(Collectors.joining(", ")); |
| 34 | + |
| 35 | +- What would be the output if the input list is ["Alice", "Bob", "Charlie"]? |
| 36 | + |
| 37 | + |
| 38 | +5. Summarizing Data |
| 39 | +------------------- |
| 40 | +- What information does Collectors.summarizingInt(x -> x) provide? |
| 41 | + |
| 42 | + IntSummaryStatistics stats = numbers.stream().collect(Collectors.summarizingInt(x -> x)); |
| 43 | + |
| 44 | +- What are the main statistical measures available from the IntSummaryStatistics object? |
| 45 | + |
| 46 | + |
| 47 | +6. Calculating Averages |
| 48 | +----------------------- |
| 49 | +- How does Collectors.averagingInt(x -> x) work compared to summarizingInt? |
| 50 | +- Why might you choose one over the other when processing numerical data? |
| 51 | + |
| 52 | + |
| 53 | +7. Counting Elements |
| 54 | +-------------------- |
| 55 | +- What is the role of Collectors.counting() in stream processing, and how is it used in this example? |
| 56 | + |
| 57 | + Long count = numbers.stream().collect(Collectors.counting()); |
| 58 | + |
| 59 | + |
| 60 | +8. Grouping Elements |
| 61 | +-------------------- |
| 62 | +- Explain how Collectors.groupingBy(String::length) groups elements from the stream. |
| 63 | +- What is the difference between these variations: |
| 64 | + * groupingBy(String::length) |
| 65 | + * groupingBy(String::length, Collectors.joining(", ")) |
| 66 | + * groupingBy(String::length, Collectors.counting()) |
| 67 | +- How does using a specific map type, as in groupingBy(String::length, TreeMap::new, Collectors.counting()), affect the result? |
| 68 | + |
| 69 | + |
| 70 | +9. Partitioning Elements |
| 71 | +------------------------ |
| 72 | +- What is the difference between grouping and partitioning in the context of stream collectors? |
| 73 | +- How does Collectors.partitioningBy(x -> x.length() > 5) split the stream into two groups? |
| 74 | + |
| 75 | + |
| 76 | +10. Mapping and Collecting |
| 77 | +-------------------------- |
| 78 | +- Describe how Collectors.mapping(x -> x.toUpperCase(), Collectors.toList()) modifies the stream before collecting. |
| 79 | +- Why might you use a mapping collector instead of applying a mapping function directly on the stream? |
| 80 | + |
| 81 | + |
| 82 | +11. Creating Maps with Collectors.toMap |
| 83 | +--------------------------------------- |
| 84 | +- In Example 5, how does Collectors.toMap(x -> x.toUpperCase(), x -> x.length()) construct a map from a stream of strings? |
| 85 | +- What will be the keys and values in the resulting map? |
| 86 | + |
| 87 | + |
| 88 | +12. Handling Duplicate Keys with toMap |
| 89 | +-------------------------------------- |
| 90 | +- In Example 6, explain the purpose of the merge function (x, y) -> x + y in: |
| 91 | + |
| 92 | + words2.stream().collect(Collectors.toMap(k -> k, v -> 1, (x, y) -> x + y)) |
| 93 | + |
| 94 | +- What issue does this merge function address when creating a map from elements with duplicate keys? |
| 95 | + |
| 96 | + |
| 97 | +13. Reduction vs. Summing Collector |
| 98 | +----------------------------------- |
| 99 | +- Compare the two approaches used to sum values in a collection: |
| 100 | + |
| 101 | + items.values().stream().reduce(Integer::sum) |
| 102 | + |
| 103 | + versus |
| 104 | + |
| 105 | + items.values().stream().collect(Collectors.summingInt(x -> x)) |
| 106 | + |
| 107 | +- What are the differences in terms of the return type and usage? |
| 108 | + |
| 109 | +These questions cover: |
| 110 | +✔ Basic collection |
| 111 | +✔ Advanced grouping/partitioning |
| 112 | +✔ Mapping, averaging, summarizing, and reduction |
| 113 | + |
| 114 | +→ Solid foundation for understanding and discussing the Java Collectors API. |
0 commit comments