An ObjectToSet provides a java.util.Map from arbitrary objects to objects of class java.util.Set.

/* * LingPipe v. 3.9 * Copyright (C) 2003-2010 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */ //package com.aliasi.util;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Iterator;importjava.util.Set; /** * An <code>ObjectToSet</code> provides a {@link java.util.Map} from * arbitrary objects to objects of class {@link java.util.Set}. * In addition to methods inherited from {@link java.util.Map}, * there are methods to add members to a set value and get * set values directly. * * @author Bob Carpenter * @version 3.8 * @since LingPipe1.0 * @param <K> the type of keys for this mapping * @param <M> the type of members stored in the value sets for this * map */publicclassObjectToSet<K,M>extendsHashMap<K,Set<M>> {staticfinallongserialVersionUID = -5758024598554958671L; /** * Construct a new object to set mapping. */publicObjectToSet() {super(); } /** * Add a member to the value of the set mapped to by * the specified key. * * @param key Key whose set value will have the object. * @param member Object to add to the value of the key. */publicvoidaddMember(K key, M member) {if(containsKey(key)) { get(key).add(member); }else{ HashSet<M> val =newHashSet<M>(); val.add(member); put(key,val); } } /** * Removes the specified value from the set of values assigned to * the specified key. If it is the last key, it removes the mapping * altogether. * * @param key Key whose members will have the value removed. * @param member Value to remove from the set of members assigned * to the key. * @return <code>true</code> if the value was in the set of * members for the key. */publicbooleanremoveMember(K key, M member) {if(!containsKey(key))returnfalse;booleanresult = get(key).remove(member);if(get(key).size() == 0) remove(key);returnresult; } /** * Adds a set of members to the value of the set mapped to by * the specified key. * * @param key Key whose set value will have the object. * @param values Values to add to the set picked out by the key. */publicvoidaddMembers(K key, Set<?extendsM> values) { Set<M> memberSet = get(key);if(memberSet == null) put(key,newHashSet<M>(values));elsememberSet.addAll(values); } /** * Returns a set constituting the union of all of the members of * the set values for all of the keys. * * @return The union of the members of the set values. */publicSet<M> memberValues() { Set<M> set =newHashSet<M>();for(Set<M> members : values()) set.addAll(members);returnset; } /** * Returns the set of values for the specified key, or the empty * set if there have been none added. This behavior only differs * from the basic {@link #get(Object)} method in that it returns the * empty set rather than <code>null</code>. */publicSet<M> getSet(K key) { Set<M> result = get(key);returnresult != null ? result :newHashSet<M>(0); } }