📄 onetomanymap.java
字号:
* @param key The key object
* @param value The value object
* @return Null.
*/
public Object put( Object key, Object value ) {
ArrayList entries = (ArrayList) m_table.get( key );
entries = entries == null ? new ArrayList() : entries;
// add the new value to the list of values held against this key
entries.add( value );
m_table.put( key, entries );
return null;
}
/**
* <p>Put all entries from one map into this map. Tests for m being a
* OneToManyMap, and, if so, copies all of the entries for each key.</p>
* @param m The map whose contents are to be copied into this map
*/
public void putAll( Map m ) {
boolean many = (m instanceof OneToManyMap);
for (Iterator i = m.keySet().iterator(); i.hasNext(); ) {
Object key = i.next();
if (many) {
for (Iterator j = ((OneToManyMap) m).getAll( key ); j.hasNext(); ) {
put( key, j.next() );
}
}
else {
put( key, m.get( key ) );
}
}
}
/**
* Remove all of the associations for the given key. If only a specific
* association is to be removed, use {@link #remove( java.lang.Object, java.lang.Object )}
* instead. Has no effect if the key is not present in the map. Since no
* single specific association with the key is defined, this method always
* returns null.
*
* @param key All associations with this key will be removed
* @return null
*/
public Object remove( Object key ) {
m_table.remove( key );
return null;
}
/**
* <p>Remove the specific association between the given key and value. Has
* no effect if the association is not present in the map. If all values
* for a particular key have been removed post removing this particular
* association, the key will no longer appear as a key in the map.</p>
*
* @param key The key object
* @param value The value object
*/
public void remove( Object key, Object value ) {
List entries = (List) m_table.get( key );
if (entries != null) {
entries.remove( value );
if (entries.isEmpty()) {
m_table.remove( key );
}
}
}
/**
* <p>Answer the number of key-value mappings in the map</p>
* @return The number of key-value pairs.
*/
public int size() {
int size = 0;
for (Iterator i = m_table.keySet().iterator(); i.hasNext(); ) {
size += ((List) m_table.get( i.next() )).size();
}
return size;
}
/**
* <p>Returns a collection view of the values contained in this map.
* Specifically, this will be a set, so duplicate values that appear
* for multiple keys are suppressed.</p>
* @return A set of the values contained in this map.
*/
public Collection values() {
Set s = CollectionFactory.createHashedSet();
for (Iterator e = m_table.keySet().iterator(); e.hasNext(); ) {
s.addAll( (List) m_table.get(e.next()) );
}
return s;
}
/**
* <p>Answer a string representation of this map. This can be quite a long string for
* large maps.<p>
*/
public String toString() {
StringBuffer buf = new StringBuffer( "OneToManyMap{" );
String sep = "";
for (Iterator i = keySet().iterator(); i.hasNext(); ) {
Object key = i.next();
buf.append( sep );
buf.append( key );
buf.append( "={" );
String sep1 = "";
for (Iterator j = getAll(key); j.hasNext(); ) {
buf.append( sep1 );
buf.append( j.next() );
sep1=",";
}
buf.append("}");
sep=",";
}
buf.append("}");
return buf.toString();
}
// Internal implementation methods
//////////////////////////////////////
// Inner classes
//////////////////////////////////////
//////////////////////////////////
//==============================================================================
// Inner class definitions
//==============================================================================
/**
* Helper class to implement the Map.Entry interface to enumerate entries in the map
*/
public static class Entry
implements Map.Entry
{
/** My key object */
private Object m_key = null;
/** My value object */
private Object m_value = null;
/**
* Constructor - save the key and value
*/
private Entry( Object key, Object value ) {
m_key = key;
m_value = value;
}
/**
* Compares the specified object with this entry for equality. Returns true if the given
* object is also a map entry and the two entries represent the same mapping.
* More formally, two entries e1 and e2 represent the same mapping if
* <code><pre>
* (e1.getKey()==null ?
* e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
* (e1.getValue()==null ?
* e2.getValue()==null : e1.getValue().equals(e2.getValue()))
* </pre></code>
*
* This ensures that the equals method works properly across different implementations of the Map.Entry interface.
*
* @param x The object to compare against
* @return True if the given object is equal to this Map.Entry object.
*/
public boolean equals( Object x ) {
if (x instanceof java.util.Map.Entry) {
Map.Entry e1 = (Map.Entry) x;
return (e1.getKey()==null ?
m_key==null : e1.getKey().equals(m_key)) &&
(e1.getValue()==null ?
m_value == null : e1.getValue().equals(m_value));
}
else
return false;
}
/**
* Answer the key for the entry
*
* @return The key object
*/
public Object getKey() {
return m_key;
}
/**
* Answer the value for the entry
*
* @return The value object
*/
public Object getValue() {
return m_value;
}
/**
* Set the value, which writes through to the map. Not implemented.
*/
public Object setValue( Object value )
throws UnsupportedOperationException
{
throw new UnsupportedOperationException( "not implemented" );
}
/**
* Returns the hash code value for this map entry.
* The hash code of a map entry e is defined to be:
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
* (e.getValue()==null ? 0 : e.getValue().hashCode())
*
* This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two
* Entries e1 and e2, as required by the general contract of Object.hashCode.
*/
public int hashCode() {
return (getKey()==null ? 0 : getKey().hashCode()) ^
(getValue()==null ? 0 : getValue().hashCode());
}
}
}
/*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -