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

net.dongliu.commons.collection.Maps Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package net.dongliu.commons.collection;

import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Function;

/**
 * Utils method for map
 *
 * @author Liu Dong
 */
public class Maps {

    /**
     * If map is null, return immutable empty map; else return self
     */
    public static  Map nullToEmpty(@Nullable Map map) {
        return map == null ? Maps.of() : map;
    }

    /**
     * Create immutable view of this Set.
     */
    public static  Map wrapImmutable(Map map) {
        return Collections.unmodifiableMap(map);
    }

    /**
     * Create a immutable map with content
     */
    public static  Map copyImmutable(Map map) {
        return Collections.unmodifiableMap(new HashMap<>(map));
    }

    /**
     * Convert map value, return new map
     */
    public static  Map convert(Map map, Function function) {
        Map newMap = Maps.create();
        map.forEach((k, v) -> newMap.put(k, function.apply(v)));
        return newMap;
    }

    /**
     * Create mutable empty map
     */
    public static  Map create() {
        return new HashMap<>();
    }

    /**
     * create mutable map
     */
    public static  Map create(K key, V value) {
        Map map = new HashMap<>();
        map.put(key, value);
        return map;
    }

    /**
     * create mutable map
     */
    public static  Map create(K key1, V value1, K key2, V value2) {
        Map map = new HashMap<>();
        map.put(key1, value1);
        map.put(key2, value2);
        return map;
    }

    /**
     * create mutable map
     */
    public static  Map create(K key1, V value1, K key2, V value2,
                                          K key3, V value3) {
        Map map = new HashMap<>();
        map.put(key1, value1);
        map.put(key2, value2);
        map.put(key3, value3);
        return map;
    }

    /**
     * create mutable map
     */
    public static  Map create(K key1, V value1, K key2, V value2,
                                          K key3, V value3, K key4, V value4) {
        Map map = new HashMap<>();
        map.put(key1, value1);
        map.put(key2, value2);
        map.put(key3, value3);
        map.put(key4, value4);
        return map;
    }

    /**
     * create mutable map
     */
    public static  Map create(K key1, V value1, K key2, V value2,
                                          K key3, V value3, K key4, V value4,
                                          K key5, V value5) {
        Map map = new HashMap<>();
        map.put(key1, value1);
        map.put(key2, value2);
        map.put(key3, value3);
        map.put(key4, value4);
        map.put(key5, value5);
        return map;
    }

    /**
     * create mutable map
     */
    public static  Map create(K key1, V value1, K key2, V value2,
                                          K key3, V value3, K key4, V value4,
                                          K key5, V value5, K key6, V value6) {
        Map map = new HashMap<>();
        map.put(key1, value1);
        map.put(key2, value2);
        map.put(key3, value3);
        map.put(key4, value4);
        map.put(key5, value5);
        map.put(key6, value6);
        return map;
    }

    /**
     * create mutable map
     */
    public static  Map create(K key1, V value1, K key2, V value2,
                                          K key3, V value3, K key4, V value4,
                                          K key5, V value5, K key6, V value6,
                                          K key7, V value7) {
        Map map = new HashMap<>();
        map.put(key1, value1);
        map.put(key2, value2);
        map.put(key3, value3);
        map.put(key4, value4);
        map.put(key5, value5);
        map.put(key6, value6);
        map.put(key7, value7);
        return map;
    }

    /**
     * Create immutable empty map
     */
    public static  Map of() {
        return Collections.emptyMap();
    }


    /**
     * create immutable map
     */
    public static  Map of(K key, V value) {
        return Collections.singletonMap(key, value);
    }

    /**
     * create immutable map
     */
    public static  Map of(K key1, V value1, K key2, V value2) {
        return wrapImmutable(create(key1, value1, key2, value2));
    }

    /**
     * create immutable map
     */
    public static  Map of(K key1, V value1, K key2, V value2,
                                      K key3, V value3) {
        return wrapImmutable(create(key1, value1, key2, value2, key3, value3));
    }

    /**
     * create immutable map
     */
    public static  Map of(K key1, V value1, K key2, V value2,
                                      K key3, V value3, K key4, V value4) {
        return wrapImmutable(create(key1, value1, key2, value2, key3, value3, key4, value4));
    }

    /**
     * create immutable map
     */
    public static  Map of(K key1, V value1, K key2, V value2,
                                      K key3, V value3, K key4, V value4,
                                      K key5, V value5) {
        return wrapImmutable(create(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5));
    }

    /**
     * create immutable map
     */
    public static  Map of(K key1, V value1, K key2, V value2,
                                      K key3, V value3, K key4, V value4,
                                      K key5, V value5, K key6, V value6) {
        return wrapImmutable(create(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5, key6, value6));
    }

    /**
     * create immutable map
     */
    public static  Map of(K key1, V value1, K key2, V value2,
                                      K key3, V value3, K key4, V value4,
                                      K key5, V value5, K key6, V value6,
                                      K key7, V value7) {
        return wrapImmutable(create(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5, key6, value6,
                key7, value7));
    }

    /**
     * Combine two list to map
     */
    public static  Map zip(List keys, List values) {
        if (keys.size() != values.size()) {
            throw new IllegalArgumentException("Key and value size not equals");
        }
        Map map = new HashMap<>();
        Iterator keyIter = keys.iterator();
        Iterator valueIter = values.iterator();
        while (keyIter.hasNext() && valueIter.hasNext()) {
            map.put(keyIter.next(), valueIter.next());
        }
        return map;
    }

    @SafeVarargs
    public static  Map createFrom(Pair... values) {
        Map map = new HashMap<>();
        for (Pair tuple : values) map.put(tuple.first(), tuple.second());
        return map;
    }

    public static  Map createFrom(Collection> values) {
        Map map = new HashMap<>();
        for (Pair tuple : values) map.put(tuple.first(), tuple.second());
        return map;
    }

    @SafeVarargs
    public static  Map from(Pair... values) {
        if (values.length == 0) {
            return of();
        }
        return wrapImmutable(createFrom(values));
    }

    public static  Map from(Collection> values) {
        return wrapImmutable(createFrom(values));
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy