Java tutorial
//package com.java2s; /* * Copyright (c) 2014 Eike Stepper (Berlin, Germany) and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Eike Stepper - initial API and implementation */ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; public class Main { public static <K, V> boolean add(Map<K, Set<V>> map, K key, V value) { Set<V> set = getSet(map, key); return set.add(value); } public static <K, V> Set<V> getSet(Map<K, Set<V>> map, K key) { Set<V> set = map.get(key); if (set == null) { set = new LinkedHashSet<V>(); map.put(key, set); } return set; } }