Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import java.util.Collections;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

public class Main {
    public static <K> Map<K, List<?>> copyNullSafeMultiHashMap(Map<? extends K, List<?>> map) {
        if (map == null)
            throw new NullPointerException("map");

        Map<K, List<?>> result = copyNullSafeMutableHashMap(map);
        for (Map.Entry<K, List<?>> entry : result.entrySet()) {
            List<?> value = entry.getValue();
            entry.setValue(Collections.unmodifiableList(new ArrayList<Object>(value)));
        }
        return result;
    }

    public static <K, V> Map<K, V> copyNullSafeMutableHashMap(Map<? extends K, ? extends V> map) {
        if (map == null)
            throw new NullPointerException("map");

        Map<K, V> result = new HashMap<K, V>(map);
        for (Map.Entry<K, V> entry : result.entrySet()) {
            if (entry.getKey() == null)
                throw new NullPointerException("entry.getKey()");
            if (entry.getValue() == null)
                throw new NullPointerException("entry.getValue()");
        }
        return result;
    }
}