Final

java.util
Class LinkedHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

public class LinkedHashMap<K,V>
extends HashMap<K,V>

LinkedHashMap is a variant on HashMap. Its entries are kept in a doubly-linked list. The iteration order is, by default, the order in which keys were inserted.

If the three arg constructor is used, and order is specified as true, the iteration would be in the order that entries were accessed. The access order gets affected by put(), get(), putAll() operations, but not by operations on the collection views.

Null elements are allowed, and all the optional Map operations are supported.

See Also:
Serialized Form

Constructor Summary
LinkedHashMap()
          Contructs a new empty instance of LinkedHashMap.
LinkedHashMap(int s)
          Constructor with specified size.
LinkedHashMap(int s, float lf)
          Constructor with specified size and load factor.
LinkedHashMap(int s, float lf, boolean order)
          Constructor with specified size, load factor and access order
LinkedHashMap(Map<? extends K,? extends V> map)
          Constructor with input map
 
Method Summary
 void clear()
          Removes all mappings from this HashMap, leaving it empty.
 Object clone()
          Answers a new HashMap with the same mappings and size as this HashMap.
 Set<Map.Entry<K,V>> entrySet()
          Answers a Set of the mappings contained in this HashMap.
 V get(Object key)
          Retrieve the map value corresponding to the given key.
 Set<K> keySet()
          Answers a Set of the keys contained in this HashMap.
 V put(K key, V value)
          Set the mapped value for the given key to the given value.
 void putAll(Map<? extends K,? extends V> map)
          Put all entries from the given map into the LinkedHashMap
 V remove(Object key)
          Remove the entry corresponding to the given key.
protected  boolean removeEldestEntry(Map.Entry eldest)
          This method is queried from the put and putAll methods to check if the eldest member of the map should be deleted before adding the new member.
 Collection<V> values()
          Answers a Collection of the values contained in this HashMap.
 
Methods inherited from class java.util.HashMap
containsKey, containsValue, isEmpty, size
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

LinkedHashMap

public LinkedHashMap()
Contructs a new empty instance of LinkedHashMap.


LinkedHashMap

public LinkedHashMap(int s)
Constructor with specified size.

Parameters:
s - Size of LinkedHashMap required

LinkedHashMap

public LinkedHashMap(int s,
                     float lf)
Constructor with specified size and load factor.

Parameters:
s - Size of LinkedHashMap required
lf - Load factor

LinkedHashMap

public LinkedHashMap(int s,
                     float lf,
                     boolean order)
Constructor with specified size, load factor and access order

Parameters:
s - Size of LinkedHashmap required
lf - Load factor
order - If true indicates that traversal order should begin with most recently accessed

LinkedHashMap

public LinkedHashMap(Map<? extends K,? extends V> map)
Constructor with input map

Parameters:
m - Input map
Method Detail

get

public V get(Object key)
Retrieve the map value corresponding to the given key.

Specified by:
get in interface Map<K,V>
Overrides:
get in class HashMap<K,V>
Parameters:
key - Key value
Returns:
mapped value or null if the key is not in the map

put

public V put(K key,
             V value)
Set the mapped value for the given key to the given value.

Specified by:
put in interface Map<K,V>
Overrides:
put in class HashMap<K,V>
Parameters:
key - Key value
value - New mapped value
Returns:
The old value if the key was already in the map or null otherwise.

putAll

public void putAll(Map<? extends K,? extends V> map)
Put all entries from the given map into the LinkedHashMap

Specified by:
putAll in interface Map<K,V>
Overrides:
putAll in class HashMap<K,V>
Parameters:
map - Input map

entrySet

public Set<Map.Entry<K,V>> entrySet()
Answers a Set of the mappings contained in this HashMap. Each element in the set is a Map.Entry. The set is backed by this HashMap so changes to one are relected by the other. The set does not support adding.

Specified by:
entrySet in interface Map<K,V>
Overrides:
entrySet in class HashMap<K,V>
Returns:
a Set of the mappings

keySet

@SuppressWarnings(value="all")
public Set<K> keySet()
Answers a Set of the keys contained in this HashMap. The set is backed by this HashMap so changes to one are relected by the other. The set does not support adding.

Specified by:
keySet in interface Map<K,V>
Overrides:
keySet in class HashMap<K,V>
Returns:
a Set of the keys

values

@SuppressWarnings(value="all")
public Collection<V> values()
Answers a Collection of the values contained in this HashMap. The collection is backed by this HashMap so changes to one are relected by the other. The collection does not support adding.

Specified by:
values in interface Map<K,V>
Overrides:
values in class HashMap<K,V>
Returns:
a Collection of the values

remove

public V remove(Object key)
Remove the entry corresponding to the given key.

Specified by:
remove in interface Map<K,V>
Overrides:
remove in class HashMap<K,V>
Parameters:
key - the key
Returns:
the value associated with the key or null if the key was no in the map

removeEldestEntry

protected boolean removeEldestEntry(Map.Entry eldest)
This method is queried from the put and putAll methods to check if the eldest member of the map should be deleted before adding the new member. If this map was created with accessOrder = true, then the result of removeEldesrEntry is assumed to be false.

Parameters:
eldest -
Returns:
true if the eldest member should be removed

clear

public void clear()
Removes all mappings from this HashMap, leaving it empty.

Specified by:
clear in interface Map<K,V>
Overrides:
clear in class HashMap<K,V>
See Also:
HashMap.isEmpty(), HashMap.size()

clone

public Object clone()
Answers a new HashMap with the same mappings and size as this HashMap.

Overrides:
clone in class HashMap<K,V>
Returns:
a shallow copy of this HashMap
See Also:
Cloneable

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2007 All Rights Reserved.