|
| 1 | +package com.baeldung.java.map.initialize; |
| 2 | + |
| 3 | +import java.util.AbstractMap; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.stream.Collectors; |
| 8 | +import java.util.stream.Stream; |
| 9 | + |
| 10 | +public class MapInitializer { |
| 11 | + |
| 12 | + public static Map<String, String> articleMapOne; |
| 13 | + static { |
| 14 | + articleMapOne = new HashMap<>(); |
| 15 | + articleMapOne.put("ar01", "Intro to Map"); |
| 16 | + articleMapOne.put("ar02", "Some article"); |
| 17 | + } |
| 18 | + |
| 19 | + public static Map<String, String> createSingletonMap() { |
| 20 | + Map<String, String> passwordMap = Collections.singletonMap("username1", "password1"); |
| 21 | + return passwordMap; |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + public Map<String, String> createEmptyMap() { |
| 26 | + Map<String, String> emptyMap = Collections.emptyMap(); |
| 27 | + return emptyMap; |
| 28 | + } |
| 29 | + |
| 30 | + public Map<String, String> createUsingDoubleBrace() { |
| 31 | + Map<String, String> doubleBraceMap = new HashMap<String, String>() { |
| 32 | + |
| 33 | + /** |
| 34 | + * |
| 35 | + */ |
| 36 | + private static final long serialVersionUID = 1L; |
| 37 | + |
| 38 | + { |
| 39 | + put("key1", "value1"); |
| 40 | + put("key2", "value2"); |
| 41 | + } |
| 42 | + }; |
| 43 | + return doubleBraceMap; |
| 44 | + } |
| 45 | + |
| 46 | + public Map<String, String> createMapUsingStreamStringArray() { |
| 47 | + Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, }) |
| 48 | + .collect(Collectors.toMap(data -> data[0], data -> data[1])); |
| 49 | + |
| 50 | + return map; |
| 51 | + } |
| 52 | + |
| 53 | + public Map<String, Integer> createMapUsingStreamObjectArray() { |
| 54 | + Map<String, Integer> map = Stream.of(new Object[][] { { "data1", 1 }, { "data2", 2 }, }) |
| 55 | + .collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1])); |
| 56 | + |
| 57 | + return map; |
| 58 | + } |
| 59 | + |
| 60 | + public Map<String, Integer> createMapUsingStreamSimpleEntry() { |
| 61 | + Map<String, Integer> map = Stream.of(new AbstractMap.SimpleEntry<>("idea", 1), new AbstractMap.SimpleEntry<>("mobile", 2)) |
| 62 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 63 | + |
| 64 | + return map; |
| 65 | + } |
| 66 | + |
| 67 | + public Map<String, Integer> createMapUsingStreamSimpleImmutableEntry() { |
| 68 | + Map<String, Integer> map = Stream.of(new AbstractMap.SimpleImmutableEntry<>("idea", 1), new AbstractMap.SimpleImmutableEntry<>("mobile", 2)) |
| 69 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 70 | + |
| 71 | + return map; |
| 72 | + } |
| 73 | + |
| 74 | + public Map<String, String> createImmutableMapWithStreams() { |
| 75 | + Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, }) |
| 76 | + .collect(Collectors.collectingAndThen(Collectors.toMap(data -> data[0], data -> data[1]), Collections::<String, String> unmodifiableMap)); |
| 77 | + return map; |
| 78 | + |
| 79 | + } |
| 80 | +} |
0 commit comments