Hi All,
Today I have released new video on "Interview Coding Questions | Convert List into Map"
This is frequently asked question in Java Interview.
Please watch full video, share, like and Subscribe Youtube channel and press bell icon. So you will get latest video notification.
|
|
|
import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.function.Function; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; public class ConvertListIntoMapDemo { public static void main(String[] args) { List<String> names = Arrays.asList( "Java", "Spring", "Docker", "Kubernetes", "AWS"); List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50); // 1. Using collect method for String Map<String, Integer> map1 = names.stream() .collect(Collectors.toMap(name -> name, name -> name.length())); System.out.println("1. Using collect method for String : "+ map1);
Map<Integer, Integer> map2 = numbers.stream() .collect(Collectors.toMap(n -> n, n -> n * n)); System.out.println("1. Using collect method for Integer : "+ map2);
// 2. Using Function.identity() method Map<String, Integer> map3 = names.stream() .collect(Collectors.toMap(Function.identity(), String::length)); System.out.println("2. Using Function.identity() method : "+ map3);
Map<Integer, Integer> map4 = numbers.stream() .collect(Collectors.toMap(Function.identity(), n -> n + n)); System.out.println("2. Using Function.identity() method : "+ map4);
// 3. Using IntStream.range and collect() method Map<Integer, String> map5 = IntStream.rangeClosed(0, names.size()-1) .boxed() .collect(Collectors.toMap(i -> i, i -> names.get(i))); System.out.println("3. Using IntStream.range and collect() method : "+ map5);
Map<Integer, String> map6 = IntStream.range(0, numbers.size()) .boxed() .collect(Collectors.toMap(n -> n, n -> n % 2 == 0 ? "Even":"Odd" )); System.out.println("3. Using IntStream.range and collect() method : "+ map6);
// 4. Using collect and handle old and new Key Map<Integer, String> map7 = names.stream() .collect(Collectors.toMap(String :: length, Function.identity(), (oldvalue, newvalue) -> oldvalue)); System.out.println("4. Using collect and handle old and new Key : "+ map7);
// 5. Using LinkedHashMap Insertion order Preservation Map<String, Integer> map8 = names.stream() .collect(Collectors.toMap(Function.identity(), String::length, (a,b) -> a, LinkedHashMap::new )); System.out.println("5. Using LinkedHashMap Insertion order Preservation: "+ map8);
// 6. Using TreeMap used for Sorting Map<String, Integer> map9 = names.stream() .collect(Collectors.toMap(Function.identity(), String::length, (a,b) -> a, TreeMap ::new )); System.out.println("6. Using TreeMap used for Sorting: "+ map9);
} } output: 1. Using collect method for String : {Java=4, Docker=6, Spring=6, AWS=3, Kubernetes=10} 1. Using collect method for Integer : {50=2500, 20=400, 40=1600, 10=100, 30=900} 2. Using Function.identity() method : {Java=4, Docker=6, Spring=6, AWS=3, Kubernetes=10} 2. Using Function.identity() method : {50=100, 20=40, 40=80, 10=20, 30=60} 3. Using IntStream.range and collect() method : {0=Java, 1=Spring, 2=Docker, 3=Kubernetes, 4=AWS} 3. Using IntStream.range and collect() method : {0=Even, 1=Odd, 2=Even, 3=Odd, 4=Even} 4. Using collect and handle old and new Key : {3=AWS, 4=Java, 6=Spring, 10=Kubernetes} 5. Using LinkedHashMap Insertion order Preservation: {Java=4, Spring=6, Docker=6, Kubernetes=10, AWS=3} 6. Using TreeMap used for Sorting: {AWS=3, Docker=6, Java=4, Kubernetes=10, Spring=6} |
0 Comments