Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

public class Main {
    public static <K, V> Map<K, V> collectionToMapAsKey(Collection<K> keyCollection) {
        return collectionToMap(keyCollection, null, new HashMap<>());
    }

    public static <K, V> Map<K, V> collectionToMap(Collection<K> keyCollection, Collection<V> valueCollection,
            Map<K, V> targetMap) {
        if (isEmpty(keyCollection)) {
            return targetMap;
        }
        if (valueCollection == null)
            valueCollection = new ArrayList<>();

        Iterator<K> keyIterator = keyCollection.iterator();
        Iterator<V> valueIterator = valueCollection.iterator();
        while (keyIterator.hasNext()) {
            V value = valueIterator.hasNext() ? valueIterator.next() : null;
            targetMap.put(keyIterator.next(), value);
        }
        return targetMap;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }
}