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

io.github.jiawade.tool.collectors.Maps Maven / Gradle / Ivy

The newest version!
package io.github.jiawade.tool.collectors;

import com.google.common.collect.Lists;

import javax.annotation.CheckForNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.google.common.base.Preconditions.checkNotNull;

public class Maps {

    public static  HashMap hashMap() {
        return new HashMap<>();
    }

    public static  HashMap hashMap(Map map) {
        return new HashMap<>(map);
    }

    public static  HashMap hashMap(List> map) {
        HashMap maps = new HashMap<>();
        map.forEach(maps::putAll);
        return maps;
    }

    public static  HashMap hashMap(K key, V value) {
        HashMap map = new HashMap<>();
        map.put(key, value);
        return map;
    }

    public static  LinkedHashMap linkedHashMap() {
        return new LinkedHashMap<>();
    }

    public static  LinkedHashMap linkedHashMap(Map map) {
        return new LinkedHashMap<>(map);
    }

    public static  LinkedHashMap linkedHashMap(K key, V value) {
        LinkedHashMap map = new LinkedHashMap<>();
        map.put(key, value);
        return map;
    }

    public static  ConcurrentMap concurrentMap() {
        return new ConcurrentHashMap<>();
    }

    public static  ConcurrentMap concurrentMap(K key, V value) {
        ConcurrentHashMap map = new ConcurrentHashMap<>();
        map.put(key, value);
        return map;
    }

    public static  TreeMap treeMap() {
        return new TreeMap();
    }

    public static  TreeMap treeMap(SortedMap map) {
        return new TreeMap<>(map);
    }

    public static  TreeMap treeMap(@CheckForNull Comparator comparator) {
        return new TreeMap<>(comparator);
    }

    public static  TreeMap treeMap(K key, V value) {
        TreeMap map = new TreeMap<>();
        map.put(key, value);
        return map;
    }

    public static  IdentityHashMap identityHashMap() {
        return new IdentityHashMap<>();
    }

    public static  IdentityHashMap identityHashMap(Map map) {
        return new IdentityHashMap<>(map);
    }

    public static  IdentityHashMap identityHashMap(K key, V value) {
        IdentityHashMap map = new IdentityHashMap<>();
        map.put(key, value);
        return map;
    }

    public static , V> EnumMap enumMap(Class type) {
        return new EnumMap<>(checkNotNull(type));
    }

    public static , V> EnumMap enumMap(Map map) {
        return new EnumMap<>(map);
    }

    public static  Map mergeCollectionsToMap(Collection keys, Collection values) {
        if (keys.isEmpty()) {
            throw new IllegalArgumentException("key size must greater than 0");
        }
        if (keys.size() != values.size()) {
            throw new IllegalArgumentException("the size of key and value must be equivalent");
        }
        List key = new ArrayList<>(keys);
        List value = new ArrayList<>(values);
        return IntStream.range(0, keys.size()).boxed()
                .collect(Collectors.toMap(key::get, value::get));
    }

    public static  Map> groupByMap(Map originMap, Map originCategoryMap) {
        return originMap.entrySet().stream()
                .collect(Collectors.groupingBy(
                        en -> Optional.ofNullable(originCategoryMap.getOrDefault(en.getKey(), null)),
                        Collectors.toMap(
                                Map.Entry::getKey,
                                Map.Entry::getValue
                        )
                ))
                .entrySet().stream()
                .collect(Collectors.toMap(en -> en.getKey().orElse(null), Map.Entry::getValue));
    }

    public static  Map> groupByValue(Map map) {
        return map.entrySet()
                .stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy