728x90
Java map collection에 대해서 알아보자
java docs, 코드레벨 까지 상세히 알아볼 예정이다.
아래와 같은 상속, 구현 구조를 가지고 있다.
- Map
- SortedMap
- NavigableMap
- TreeMap
- Hashtable
- HashMap
- LinkedHashMap
- NavigableMap
- SortedMap
Map
맵은 중복된 키를 가질 수 없다. 각각의 키는 하나의 밸류에 매핑된다.
이러한 인터페이스는 Dictionary 추상클래스를 대체한다.
* An object that maps keys to values. A map cannot contain duplicate keys;
* each key can map to at most one value.
*
* <p>This interface takes the place of the <tt>Dictionary</tt> class, which
* was a totally abstract class rather than an interface.
* <p>This interface takes the place of the <tt>Dictionary</tt> class, which
* was a totally abstract class rather than an interface.
*
* <p>The <tt>Map</tt> interface provides three <i>collection views</i>, which
* allow a map's contents to be viewed as a set of keys, collection of values,
* or set of key-value mappings. The <i>order</i> of a map is defined as
* the order in which the iterators on the map's collection views return their
* elements. Some map implementations, like the <tt>TreeMap</tt> class, make
* specific guarantees as to their order; others, like the <tt>HashMap</tt>
* class, do not.
public interface Map<K,V>
SortedMap
1.2부터 제공됨.
* A {@link Map} that further provides a <em>total ordering</em> on its keys.
* The map is ordered according to the {@linkplain Comparable natural
* ordering} of its keys, or by a {@link Comparator} typically
* provided at sorted map creation time. This order is reflected when
* iterating over the sorted map's collection views (returned by the
* {@code entrySet}, {@code keySet} and {@code values} methods).
* Several additional operations are provided to take advantage of the
* ordering. (This interface is the map analogue of {@link SortedSet}.)
*
public interface SortedMap<K,V> extends Map<K,V>
NavigableMap
1.6부터 제공됨.
public interface NavigableMap<K,V> extends SortedMap<K,V>
* A {@link SortedMap} extended with navigation methods returning the
* closest matches for given search targets. Methods
* {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry},
* and {@code higherEntry} return {@code Map.Entry} objects
* associated with keys respectively less than, less than or equal,
* greater than or equal, and greater than a given key, returning
* {@code null} if there is no such key. Similarly, methods
* {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and
* {@code higherKey} return only the associated keys. All of these
* methods are designed for locating, not traversing entries.
*
TreeMap
1.2부터 제공됨.
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
* A Red-Black tree based {@link NavigableMap} implementation.
* The map is sorted according to the {@linkplain Comparable natural
* ordering} of its keys, or by a {@link Comparator} provided at map
* creation time, depending on which constructor is used.
*
Hashtable
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable
* This class implements a hash table, which maps keys to values. Any
* non-<code>null</code> object can be used as a key or as a value. <p>
*
* To successfully store and retrieve objects from a hashtable, the
* objects used as keys must implement the <code>hashCode</code>
* method and the <code>equals</code> method. <p>
*
HashMap
1.2부터 제공
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
* Hash table based implementation of the <tt>Map</tt> interface. This
* implementation provides all of the optional map operations, and permits
* <tt>null</tt> values and the <tt>null</tt> key. (The <tt>HashMap</tt>
* class is roughly equivalent to <tt>Hashtable</tt>, except that it is
* unsynchronized and permits nulls.) This class makes no guarantees as to
* the order of the map; in particular, it does not guarantee that the order
* will remain constant over time.
LinkedHashMap
1.4부터 제공
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
* <p>Hash table and linked list implementation of the <tt>Map</tt> interface,
* with predictable iteration order. This implementation differs from
* <tt>HashMap</tt> in that it maintains a doubly-linked list running through
* all of its entries. This linked list defines the iteration ordering,
* which is normally the order in which keys were inserted into the map
* (<i>insertion-order</i>). Note that insertion order is not affected
* if a key is <i>re-inserted</i> into the map. (A key <tt>k</tt> is
* reinserted into a map <tt>m</tt> if <tt>m.put(k, v)</tt> is invoked when
* <tt>m.containsKey(k)</tt> would return <tt>true</tt> immediately prior to
* the invocation.)