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

net.lenni0451.commons.collections.Maps Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package net.lenni0451.commons.collections;

import lombok.experimental.UtilityClass;

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@UtilityClass
public class Maps {

    /**
     * Sort a map using the given comparator.
* This will return a {@link LinkedHashMap} to keep the order of the map. * * @param map The map to sort * @param comparator The comparator to use * @param The key type * @param The value type * @return The sorted map */ public static LinkedHashMap sort(final Map map, final Comparator> comparator) { return map.entrySet().stream() .sorted(comparator) .collect(Collectors., K, V, LinkedHashMap>toMap(Map.Entry::getKey, Map.Entry::getValue, (k, v) -> { throw new IllegalStateException("Duplicate key '" + k + "' (" + v + ")"); }, LinkedHashMap::new)); } /** * Merge multiple maps into one.
* The maps are not modified. * * @param map The first map * @param others The other maps * @param The key type * @param The value type * @return The merged map */ @SafeVarargs public static Map merge(final Map map, final Map... others) { Map newMap = new HashMap<>(map); for (Map other : others) newMap.putAll(other); return newMap; } /** * Create a new map with the given objects.
* The objects must be in the format: {@code key, value, key, value, ...}
* The types of the keys and values are not checked. * * @param mapSupplier The supplier to create the map * @param objects The objects to add to the map * @param The map type * @param The key type * @param The value type * @return The created map */ public static , K, V> T any(final Supplier mapSupplier, final Object... objects) { if (objects.length % 2 != 0) throw new IllegalArgumentException("Uneven object count"); T map = mapSupplier.get(); for (int i = 0; i < objects.length; i += 2) map.put((K) objects[i], (V) objects[i + 1]); return map; } /** * Create a new map which is passed to the given consumer. * * @param mapSupplier The supplier to create the map * @param mapConsumer The consumer to pass the map to * @param The map type * @param The key type * @param The value type * @return The created map */ public static , K, V> T any(final Supplier mapSupplier, final Consumer mapConsumer) { T map = mapSupplier.get(); mapConsumer.accept(map); return map; } /** * Create a new {@link HashMap} with the given objects.
* The objects must be in the format: {@code key, value, key, value, ...}
* The types of the keys and values are not checked. * * @param objects The objects to add to the map * @param The key type * @param The value type * @return The created map */ public static HashMap hashMap(final Object... objects) { return any(HashMap::new, objects); } /** * Create a new {@link HashMap} which is passed to the given consumer. * * @param mapConsumer The consumer to pass the map to * @param The key type * @param The value type * @return The created map */ public static HashMap hashMap(final Consumer> mapConsumer) { return any(HashMap::new, mapConsumer); } /** * Create a new {@link LinkedHashMap} with the given objects.
* The objects must be in the format: {@code key, value, key, value, ...}
* The types of the keys and values are not checked. * * @param objects The objects to add to the map * @param The key type * @param The value type * @return The created map */ public static LinkedHashMap linkedHashMap(final Object... objects) { return any(LinkedHashMap::new, objects); } /** * Create a new {@link LinkedHashMap} which is passed to the given consumer. * * @param mapConsumer The consumer to pass the map to * @param The key type * @param The value type * @return The created map */ public static LinkedHashMap linkedHashMap(final Consumer> mapConsumer) { return any(LinkedHashMap::new, mapConsumer); } /** * Create a new {@link ConcurrentHashMap} with the given objects.
* The objects must be in the format: {@code key, value, key, value, ...}
* The types of the keys and values are not checked. * * @param objects The objects to add to the map * @param The key type * @param The value type * @return The created map */ public static ConcurrentHashMap concurrentHashMap(final Object... objects) { return any(ConcurrentHashMap::new, objects); } /** * Create a new {@link ConcurrentHashMap} which is passed to the given consumer. * * @param mapConsumer The consumer to pass the map to * @param The key type * @param The value type * @return The created map */ public static ConcurrentHashMap concurrentHashMap(final Consumer> mapConsumer) { return any(ConcurrentHashMap::new, mapConsumer); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy