Last active
November 15, 2018 20:08
-
-
Save nbuesing/ad311f3a83b6a4c51bc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.objectpartners.buesing.util; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
/** | |
* @author Neil Buesing | |
*/ | |
public final class MapUtils { | |
public static class Entry<K, V> { | |
private K key; | |
private V value; | |
public Entry(final K key, final V value) { | |
this.key = key; | |
this.value = value; | |
} | |
public K getKey() { | |
return key; | |
} | |
public V getValue() { | |
return value; | |
} | |
} | |
public static <K, V> Entry<K, V> entry(final K key, final V value) { | |
return new Entry(key, value); | |
} | |
public static <K, V> Map<K, V> asMap(final Entry<K, V>... entries) { | |
return populate(new HashMap<K, V>(), entries); | |
} | |
public static <K, V> Map<K, V> asOrderedMap(final Entry<K, V>... entries) { | |
return populate(new LinkedHashMap<K, V>(), entries); | |
} | |
public static <K, V> Map<K, V> asUnmodifiableMap(final Entry<K, V>... entries) { | |
return Collections.unmodifiableMap(populate(new HashMap<K, V>(), entries)); | |
} | |
public static <K, V> Map<K, V> asUnmodifiableOrderedMap(final Entry<K, V>... entries) { | |
return Collections.unmodifiableMap(populate(new LinkedHashMap<K, V>(), entries)); | |
} | |
private static <K, V> Map<K, V> populate(final Map map, final Entry<K, V>... entries) { | |
for (final Entry entry : entries) { | |
map.put(entry.getKey(), entry.getValue()); | |
} | |
return map; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment