-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListToMap.java
More file actions
36 lines (30 loc) · 1.49 KB
/
Copy pathListToMap.java
File metadata and controls
36 lines (30 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package maps;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ListToMap {
// ## **✅ Example: Using `boxed()` to Convert `IntStream` to `Stream<Integer>`**
// ```java
// ### **🚀 Understanding `boxed()` in Java Streams**
// 📌 **What is `boxed()` in Java Streams?**
// - In Java Streams, **`IntStream`, `LongStream`, and `DoubleStream` are
// primitive streams**.
// - The method **`boxed()` converts primitive types (`int`, `long`, `double`)
// to their wrapper classes (`Integer`, `Long`, `Double`).
// - This is needed when working with **Collectors, Maps, or Lists that require
// Objects**.
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(101, 102, 103);
List<String> stringList = Arrays.asList("Apple", "Banana", "Cherry");
// Convert Two Lists to a Map<String, Integer>
Map<String, Integer> stringIntegerMap = IntStream.range(0, integerList.size()) // Creates IntStream (primitive)
.boxed() // Converts IntStream (int) to Stream<Integer> (Object)
.collect(Collectors.toMap(stringList::get, integerList::get));
System.out.println("Final Map: " + stringIntegerMap);
// Using for loop
Map<String, Integer> stringIntegerMap1 = new HashMap<>();
for (int i = 0; i < integerList.size(); i++) {
stringIntegerMap1.put(stringList.get(i), integerList.get(i));
}
}
}