Java List Combine combineLists(List keys, List values)

Here you can find the source of combineLists(List keys, List values)

Description

Combines two lists into map, using first list as keys and second as values

License

Apache License

Parameter

Parameter Description
keys List used as keys
values List used as values
K Type of values in keys list
V Type of values in values list

Declaration

public static <K, V> Map<K, V> combineLists(List<? extends K> keys, List<? extends V> values) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {
    /**// ww  w .  j  av a2s  .  c o m
     * Combines two lists into map, using first list as keys and second as values
     *
     * @param keys   List used as keys
     * @param values List used as values
     * @param <K>    Type of values in keys list
     * @param <V>    Type of values in values list
     * @return {@link LinkedHashMap}
     */
    public static <K, V> Map<K, V> combineLists(List<? extends K> keys, List<? extends V> values) {
        if (keys.size() != values.size()) {
            throw new IllegalArgumentException("Cannot combine lists with dissimilar sizes");
        }

        if (keys.isEmpty() && values.isEmpty()) {
            return Collections.emptyMap();
        }

        int size = values.size();

        Map<K, V> map = new LinkedHashMap<>(size);
        for (int i = 0; i < size; i++) {
            map.put(keys.get(i), values.get(i));
        }

        return map;
    }
}

Related

  1. combineAsLists(Object one, Object two)
  2. combineAux(List> collections, List objectAccumulator, List> resultList)
  3. combineFloat(List nums)
  4. combineLines(List lines)
  5. combineList(List l1, List l2)
  6. combineLists(List a, List b)
  7. combineString(List lstInput, String strToken)
  8. combineStringList(List sList)
  9. combineSubtractArrays(List a1, List a2)