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

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

Go to download

Commons for lang, crypto, xml, dom, text, csv, reflection, annotations, parsing, ...

There is a newer version: 4.4.0
Show newest version
/*
 * tksCommons
 * 
 * Author  : Thomas Kuhlmann (ThK-Systems, http://www.thk-systems.de)
 * License : LGPL (https://www.gnu.org/licenses/lgpl.html)
 */

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