Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 {
    /**
     * 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;
    }
}