de.thksystems.util.collection.MapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tkscommons Show documentation
Show all versions of tkscommons Show documentation
Commons for lang, crypto, dom, text, csv, reflection, parsing, xtreams...
package de.thksystems.util.collection;
import java.util.HashMap;
public final class MapUtils {
private MapUtils() {
}
/**
* Creates a {@link HashMap} of {@link String},{@link String} by reading key-value-pairs like 'key::value'.
*/
public static HashMap createHashMap(String... values) {
return createHashMapWithDelimiter("::", values);
}
/**
* Creates a {@link HashMap} of {@link String},{@link String} by reading key-value-pairs like 'key[delimiter]value'.
*/
public static HashMap createHashMapWithDelimiter(String delimiter, String... values) {
HashMap map = new HashMap<>(values.length);
for (String value : values) {
String[] pair = value.split(delimiter);
if (pair.length != 2 || pair[0].length() == 0 || pair[1].length() == 0) {
throw new IllegalArgumentException("Invalid key-value-pair: " + value);
}
if (map.containsKey(pair[0])) {
throw new IllegalArgumentException("Duplicate key: " + pair[0]);
}
map.put(pair[0], pair[1]);
}
return map;
}
}