Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import java.util.Map;

public class Main {
    public static <K, V> Map<K, V> copyNullSafeHashMapWithNullValues(Map<? extends K, ? extends V> map) {
        if (map.isEmpty()) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(copyNullSafeMutableHashMapWithNullValues(map));
    }

    public static <K, V> Map<K, V> copyNullSafeMutableHashMapWithNullValues(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()");
        }
        return result;
    }
}