All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.thksystems.util.collection.MapUtils Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
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;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy