Java 16961 (#13236)
* [JAVA-16961] Moved code for article "Create an Empty Map in Java" * [JAVA-16961] Alter README.md * [JAVA-16961] Moved code for article "Sorting a Hashset in Java" * [JAVA-16961] Added links to README.md files * [JAVA-16961] Revert link changes * [JAVA-16961] Replaced junit4 with junit5 annotations * [JAVA-16961] test build * [JAVA-16961] Added junit annotations * [JAVA-16961] Added links Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.map;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.NavigableMap;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class EmptyMapInitializer {
|
||||
|
||||
public static Map<String, String> articleMap;
|
||||
static {
|
||||
articleMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public static Map<String, String> createEmptyMap() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
public void createMapUsingConstructors() {
|
||||
Map hashMap = new HashMap();
|
||||
Map linkedHashMap = new LinkedHashMap();
|
||||
Map treeMap = new TreeMap();
|
||||
}
|
||||
|
||||
public Map<String, String> createEmptyMapUsingMapsObject() {
|
||||
Map<String, String> emptyMap = Maps.newHashMap();
|
||||
return emptyMap;
|
||||
}
|
||||
|
||||
public Map createGenericEmptyMapUsingGuavaMapsObject() {
|
||||
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
|
||||
return genericEmptyMap;
|
||||
}
|
||||
|
||||
public static Map<String, String> createMapUsingGuava() {
|
||||
Map<String, String> emptyMapUsingGuava =
|
||||
Maps.newHashMap(ImmutableMap.of());
|
||||
return emptyMapUsingGuava;
|
||||
}
|
||||
|
||||
public static Map<String, String> createImmutableMapUsingGuava() {
|
||||
Map<String, String> emptyImmutableMapUsingGuava = ImmutableMap.of();
|
||||
return emptyImmutableMapUsingGuava;
|
||||
}
|
||||
|
||||
public SortedMap<String, String> createEmptySortedMap() {
|
||||
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
|
||||
return sortedMap;
|
||||
}
|
||||
|
||||
public NavigableMap<String, String> createEmptyNavigableMap() {
|
||||
NavigableMap<String, String> navigableMap = Collections.emptyNavigableMap();
|
||||
return navigableMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EmptyMapInitializerUnitTest {
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void givenEmptyMap_whenAddingEntries_throwsException() {
|
||||
Map<String, String> map = EmptyMapInitializer.createEmptyMap();
|
||||
map.put("key", "value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyMap_whenChecked_returnsTrue() {
|
||||
assertTrue(EmptyMapInitializer.articleMap.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() {
|
||||
Map<String, String> emptyMapUsingGuava =
|
||||
EmptyMapInitializer.createMapUsingGuava();
|
||||
assertTrue(emptyMapUsingGuava.isEmpty());
|
||||
emptyMapUsingGuava.put("key", "value");
|
||||
assertFalse(emptyMapUsingGuava.isEmpty());
|
||||
}
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void givenImmutableEmptyMapUsingGuava_whenAddingEntries_throwsException() {
|
||||
Map<String, String> map = EmptyMapInitializer.createImmutableMapUsingGuava();
|
||||
map.put("key", "value");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user