Example usage for java.util.stream Collectors toMap

List of usage examples for java.util.stream Collectors toMap

Introduction

In this page you can find the example usage for java.util.stream Collectors toMap.

Prototype

public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
        BinaryOperator<U> mergeFunction, Supplier<M> mapFactory) 

Source Link

Document

Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.

Usage

From source file:Main.java

public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper) {
    return Collectors.toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
}

From source file:Main.java

public static <T, K, U> Collector<T, ?, TreeMap<K, U>> toTreeMap(Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper) {
    return Collectors.toMap(keyMapper, valueMapper, throwingMerger(), TreeMap::new);
}

From source file:Main.java

public static <T, K, U> Collector<T, ?, LinkedHashMap<K, U>> toLinkedHashMap(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
    return Collectors.toMap(keyMapper, valueMapper, (u, v) -> {
        throw new IllegalStateException(String.format("Duplicate key %s", u));
    }, LinkedHashMap::new);
}

From source file:Main.java

public static <T, K, M extends Map<K, T>> M toMap(Collection<T> entities, Function<T, K> keyMapper,
        Supplier<M> supplier) {
    return entities.stream().collect(Collectors.toMap(keyMapper, Function.identity(), (a, b) -> b, supplier));
}

From source file:com.thoughtworks.go.agent.common.util.HeaderUtil.java

public static Map<String, String> parseExtraProperties(Header extraPropertiesHeader) {
    if (extraPropertiesHeader == null || StringUtils.isBlank(extraPropertiesHeader.getValue())) {
        return new HashMap<>();
    }/*from ww  w  .j a v a  2 s  .c om*/

    try {
        final String headerValue = new String(Base64.decodeBase64(extraPropertiesHeader.getValue()), UTF_8);

        if (StringUtils.isBlank(headerValue)) {
            return new HashMap<>();
        }

        return Arrays.stream(headerValue.trim().split(" +")).map(property -> property.split("="))
                .collect(Collectors.toMap(keyAndValue -> keyAndValue[0].replaceAll("%20", " "),
                        keyAndValue -> keyAndValue[1].replaceAll("%20", " "), (value1, value2) -> value1,
                        LinkedHashMap::new));
    } catch (Exception e) {
        LOGGER.warn("Failed to parse extra properties header value: {}", extraPropertiesHeader.getValue(), e);
        return new HashMap<>();
    }
}

From source file:Main.java

public static <T, K, V, M extends Map<K, V>> M toMap(Collection<T> entities, Function<T, K> keyMapper,
        Function<T, V> valueMapper, Supplier<M> supplier) {
    return entities.stream().collect(Collectors.toMap(keyMapper, valueMapper, (a, b) -> b, supplier));
}

From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVAutoDetect.java

private static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValue(Map<K, V> map) {
    return map.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(
            Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}

From source file:cn.edu.zjnu.acm.judge.service.LanguageService.java

public Map<Integer, Language> getAvailableLanguages() {
    return languageMapper.findAll().stream().collect(
            Collectors.toMap(Language::getId, Function.identity(), throwOnMerge(), LinkedHashMap::new));
}

From source file:com.intuit.quickbase.MergeStatService.java

public void retrieveCountryPopulationList() {

    DBStatService dbStatService = new DBStatService();
    List<Pair<String, Integer>> dbList = dbStatService.GetCountryPopulations();

    ConcreteStatService conStatService = new ConcreteStatService();
    List<Pair<String, Integer>> apiList = conStatService.GetCountryPopulations();

    //Use of Predicate Interface
    Predicate<Pair<String, Integer>> pred = (pair) -> pair != null;

    if (apiList != null) {
        // Converting the keys of each element in the API list to lowercase
        List<Pair<String, Integer>> modifiedAPIList = apiList.stream().filter(pred)
                .map(p -> new ImmutablePair<String, Integer>(p.getKey().toLowerCase(), p.getValue()))
                .collect(Collectors.toList());
        //modifiedAPIList.forEach(pair -> System.out.println("key: " + pair.getLeft() + ": value: " + pair.getRight()));                      

        if (dbList != null) {
            // Merge two list and remove duplicates
            Collection<Pair<String, Integer>> result = Stream.concat(dbList.stream(), modifiedAPIList.stream())
                    .filter(pred)/*from   w  ww  . ja va2s. c o  m*/
                    .collect(Collectors.toMap(Pair::getLeft, p -> p, (p, q) -> p, LinkedHashMap::new)).values();

            // Need to Convert collection to List
            List<Pair<String, Integer>> merge = new ArrayList<>(result);
            merge.forEach(pair -> System.out.println("key: " + pair.getKey() + ": value: " + pair.getValue()));

        } else {
            System.out.println("Country list retrieved form database is empty");
        }
    } else {
        System.out.println("Country list retrieved form API is empty");
    }

    //            if(apiList != null) {
    //                Iterator itr = apiList.iterator();
    //                for(Pair<String, Integer> pair : apiList){                    
    //                    //System.out.println("key: " + pair.getLeft() + ": value: " + pair.getRight());
    //                }                
    //            }    

}

From source file:org.fede.calculator.config.AppConfig.java

public static <K, U> Collector<Map.Entry<K, U>, ?, Map<K, U>> entriesToLinkedHashMap() {
    return Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue(), (v1, v2) -> v1, LinkedHashMap::new);
}