|
| 1 | +package com.hellokoding.java.collections; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +import static java.util.Map.Entry.comparingByKey; |
| 8 | +import static java.util.Map.Entry.comparingByValue; |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +public class LinkedHashMapTest { |
| 12 | + @Test |
| 13 | + public void declare() { |
| 14 | + Map<String, Integer> linkedHashMap1 = new LinkedHashMap<>(); |
| 15 | + assertThat(linkedHashMap1).isInstanceOf(LinkedHashMap.class); |
| 16 | + |
| 17 | + LinkedHashMap<String, Integer> linkedHashMap2 = new LinkedHashMap<>(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void initInOneLineWithFactoryMethods() { |
| 22 | + // create and initialize a HashMap from Java 9+ Map.of |
| 23 | + Map<String, Integer> linkedHashMap1 = new LinkedHashMap<>((Map.of("k1", 1, "k3", 2, "k2", 3))); |
| 24 | + assertThat(linkedHashMap1).hasSize(3); |
| 25 | + |
| 26 | + // create and initialize a HashMap from Java 9+ Map.ofEntries |
| 27 | + Map<String, Integer> linkedHashMap2 = new LinkedHashMap<>(Map.ofEntries(Map.entry("k4", 4), Map.entry("k5", 5))); |
| 28 | + assertThat(linkedHashMap2).hasSize(2); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void initializeWithPutIfAbsent() { |
| 33 | + // Create a new LinkedHashMap |
| 34 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(); |
| 35 | + |
| 36 | + // Add elements to LinkedHashMap |
| 37 | + linkedHashMap.putIfAbsent("k1", 1); |
| 38 | + linkedHashMap.putIfAbsent("k2", 2); |
| 39 | + linkedHashMap.putIfAbsent("k3", 3); |
| 40 | + |
| 41 | + // Can add null key and value |
| 42 | + linkedHashMap.putIfAbsent(null, 4); |
| 43 | + linkedHashMap.putIfAbsent("k4", null); |
| 44 | + |
| 45 | + // Duplicate key will be ignored |
| 46 | + linkedHashMap.putIfAbsent("k1", 10); |
| 47 | + assertThat(linkedHashMap).hasSize(5); |
| 48 | + |
| 49 | + // The output ordering is predictable as LinkedHashMap is reserved the insertion order |
| 50 | + System.out.println(linkedHashMap); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void iterateOverKeyValuePairs() { |
| 55 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.of("k1", 1, "k2", 2)); |
| 56 | + linkedHashMap.forEach((k, v) -> System.out.printf("%s=%d ", k, v)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void iterateOverEntrySetKeySetAndValues() { |
| 61 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.of("k1", 1, "k2", 2)); |
| 62 | + linkedHashMap.entrySet().forEach(e -> System.out.printf("%s ", e)); |
| 63 | + linkedHashMap.keySet().forEach(k -> System.out.printf("%s ", k)); |
| 64 | + linkedHashMap.values().forEach(v -> System.out.printf("%s ", v)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void retrieve() { |
| 69 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.of("k1", 1, "k2", 2)); |
| 70 | + |
| 71 | + Set<Map.Entry<String, Integer>> entrySet = linkedHashMap.entrySet(); |
| 72 | + assertThat(entrySet).contains(Map.entry("k1", 1), Map.entry("k2", 2)); |
| 73 | + |
| 74 | + Set<String> keySet = linkedHashMap.keySet(); |
| 75 | + assertThat(keySet).contains("k1", "k2"); |
| 76 | + |
| 77 | + Collection<Integer> values = linkedHashMap.values(); |
| 78 | + assertThat(values).contains(1, 2); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void getValueByKey() { |
| 83 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.of("k1", 1, "k2", 2)); |
| 84 | + int value = linkedHashMap.get("k1"); |
| 85 | + |
| 86 | + assertThat(value).isEqualTo(1); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void filter() { |
| 91 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.of("k1", 1, "k2", 2)); |
| 92 | + Integer[] arr = linkedHashMap.values().stream().filter(v -> v > 1).toArray(Integer[]::new); |
| 93 | + assertThat(arr).contains(2); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void containsPutReplaceRemove() { |
| 98 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.of("k1", 1, "k2", 2)); |
| 99 | + |
| 100 | + boolean containedKey = linkedHashMap.containsKey("k1"); |
| 101 | + assertThat(containedKey).isTrue(); |
| 102 | + |
| 103 | + boolean containedValue = linkedHashMap.containsValue(2); |
| 104 | + assertThat(containedValue).isTrue(); |
| 105 | + |
| 106 | + linkedHashMap.put("k3", 3); |
| 107 | + assertThat(linkedHashMap).hasSize(3); |
| 108 | + |
| 109 | + linkedHashMap.replace("k1", 10); |
| 110 | + assertThat(linkedHashMap).contains(Map.entry("k1", 10), Map.entry("k2", 2), Map.entry("k3", 3)); |
| 111 | + |
| 112 | + linkedHashMap.remove("k3"); |
| 113 | + assertThat(linkedHashMap).contains(Map.entry("k1", 10), Map.entry("k2", 2)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void objectsComparingProblem(){ |
| 118 | + Map<Category, Book> linkedHashMap = new LinkedHashMap<>(); |
| 119 | + |
| 120 | + linkedHashMap.put(new Category(1, "c1"), new Book(1, "b1")); |
| 121 | + |
| 122 | + boolean containedKey = linkedHashMap.containsKey(new Category(1, "c1")); |
| 123 | + assertThat(containedKey).isFalse(); |
| 124 | + |
| 125 | + boolean containedValue = linkedHashMap.containsValue(new Book(1, "b1")); |
| 126 | + assertThat(containedValue).isFalse(); |
| 127 | + |
| 128 | + linkedHashMap.put(new Category(1, "c1"), new Book(1, "b1")); |
| 129 | + assertThat(linkedHashMap).hasSize(2); |
| 130 | + |
| 131 | + Book previousValue = linkedHashMap.replace(new Category(1, "c1"), new Book(2, "b1")); |
| 132 | + assertThat(previousValue).isNull(); |
| 133 | + |
| 134 | + linkedHashMap.remove(new Category(1, "c1")); |
| 135 | + assertThat(linkedHashMap).hasSize(2); |
| 136 | + } |
| 137 | + |
| 138 | + static class Category { |
| 139 | + int id; |
| 140 | + String name; |
| 141 | + |
| 142 | + Category(int id, String name) { |
| 143 | + this.id = id; |
| 144 | + this.name = name; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + static class Book { |
| 149 | + int id; |
| 150 | + String title; |
| 151 | + |
| 152 | + Book(int id, String title) { |
| 153 | + this.id = id; |
| 154 | + this.title = title; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void objectsComparingFixed(){ |
| 160 | + Map<CategoryFixed, BookFixed> linkedHashMap = new LinkedHashMap<>(); |
| 161 | + |
| 162 | + linkedHashMap.put(new CategoryFixed(1, "c1"), new BookFixed(1, "b1")); |
| 163 | + |
| 164 | + boolean containedKey = linkedHashMap.containsKey(new CategoryFixed(1, "c1")); |
| 165 | + assertThat(containedKey).isTrue(); |
| 166 | + |
| 167 | + boolean containedValue = linkedHashMap.containsValue(new BookFixed(1, "b1")); |
| 168 | + assertThat(containedValue).isTrue(); |
| 169 | + |
| 170 | + linkedHashMap.put(new CategoryFixed(1, "c1"), new BookFixed(1, "b1")); |
| 171 | + assertThat(linkedHashMap).hasSize(1); |
| 172 | + |
| 173 | + BookFixed previousValue = linkedHashMap.replace(new CategoryFixed(1, "c1"), new BookFixed(2, "b1")); |
| 174 | + assertThat(previousValue).isNotNull(); |
| 175 | + |
| 176 | + linkedHashMap.remove(new CategoryFixed(1, "c1")); |
| 177 | + assertThat(linkedHashMap).hasSize(0); |
| 178 | + } |
| 179 | + |
| 180 | + static class CategoryFixed { |
| 181 | + int id; |
| 182 | + String name; |
| 183 | + |
| 184 | + CategoryFixed(int id, String name) { |
| 185 | + this.id = id; |
| 186 | + this.name = name; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public boolean equals(Object o) { |
| 191 | + if (this == o) return true; |
| 192 | + if (o == null || getClass() != o.getClass()) return false; |
| 193 | + CategoryFixed that = (CategoryFixed) o; |
| 194 | + return id == that.id && |
| 195 | + Objects.equals(name, that.name); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public int hashCode() { |
| 200 | + return Objects.hash(id, name); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + static class BookFixed { |
| 205 | + int id; |
| 206 | + String title; |
| 207 | + |
| 208 | + BookFixed(int id, String title) { |
| 209 | + this.id = id; |
| 210 | + this.title = title; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public boolean equals(Object o) { |
| 215 | + if (this == o) return true; |
| 216 | + if (o == null || getClass() != o.getClass()) return false; |
| 217 | + BookFixed bookFixed = (BookFixed) o; |
| 218 | + return id == bookFixed.id && |
| 219 | + Objects.equals(title, bookFixed.title); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public int hashCode() { |
| 224 | + return Objects.hash(id, title); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + public void sortByKeysAndByValues_WithArrayListAndComparator() { |
| 230 | + Map.Entry<String, Integer> e1 = Map.entry("k1", 1); |
| 231 | + Map.Entry<String, Integer> e2 = Map.entry("k2", 20); |
| 232 | + Map.Entry<String, Integer> e3 = Map.entry("k3", 3); |
| 233 | + |
| 234 | + Map<String, Integer> linkedHashMap = new LinkedHashMap<>(Map.ofEntries(e3, e1, e2)); |
| 235 | + |
| 236 | + List<Map.Entry<String, Integer>> arrayList1 = new ArrayList<>(linkedHashMap.entrySet()); |
| 237 | + arrayList1.sort(comparingByKey(Comparator.naturalOrder())); |
| 238 | + assertThat(arrayList1).containsExactly(e1, e2, e3); |
| 239 | + |
| 240 | + List<Map.Entry<String, Integer>> arrayList2 = new ArrayList<>(linkedHashMap.entrySet()); |
| 241 | + arrayList2.sort(comparingByValue(Comparator.reverseOrder())); |
| 242 | + assertThat(arrayList2).containsExactly(e2, e3, e1); |
| 243 | + } |
| 244 | +} |
0 commit comments