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.Collection;

import java.util.Map;

public class Main {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <K, V extends Collection<T>, T> void addToMap(Map<K, V> data, K key, T value,
            Class<? extends Collection> clz) {
        V values = data.get(key);
        if (values == null) {
            try {
                values = (V) clz.newInstance();
            } catch (InstantiationException e) {
                throw new IllegalArgumentException("Failed to create collection class", e);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException("Failed to create collection class", e);
            }

            data.put(key, values);
        }

        values.add(value);
    }
}