Today we are gonna have a brief view over the Collection and Map interfaces in Java:
As following diagram shows, while they are all used to save a group of objects of same types, Sets, Lists, Queues and Deques belong to the Collection interface, whereas Maps have their own interface.
Reference: https://www.codejava.net/java-core/collections/java-map-collection-tutorial-and-examples
----------------------------------------------------------------------------------------------------------------------
Collection
root interface of collection framework. Its has following sub-interfaces- Set:
- EnumSet:
- HashSet: Fastest but no ordering guarantees. O(1) for most operations. Initial capacity and load factor.
- LinkedHashSet: Speed between HashSet and TreeSet. Provides insertion-ordered iteration.
- TreeSet: O(logn).
- List:
- Queue:
- Deque: LIFO interface
Properties of different Collections:
Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable.
Lists that support fast (generally constant time) indexed element access are known as random access lists.
Lists that do not support fast indexed element access are known as sequential access lists.
p.s.
1. Great care must be exercised when using mutable elements in a Collection, because the behavior of equals is undefined when the property of element is changed.
2. The contains() method would call equals(), which has following rules:
- reflexive: an object must equal itself
- symmetric: x.equals(y) must return the same result as y.equals(x)
- transitive: if x.equals(y) and y.equals(z) then also x.equals(z)
- consistent: the value of equals() should change only if a property that is contained in equals() changes (no randomness allowed)
- internal consistency: the value of hashCode() may only change if a property that is in equals() changes equals
- consistency: objects that are equal to each other must return the same hashCode
- collisions: unequal objects may have the same hashCode
Map
Cannot contain duplicate keys; each key can map to at most one value. Map provides 3 different views: (If map modified during the iteration, the results of these methods are undefined.)- set of keys: keySet()
- collection of values: values()
- set of key-value mappings: entrySet()
- The order of a map = iterators return order. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.
- If mutable objects are used as keys, beware that undefined behavior by equals() method may appear if the values of an object changed in such a map.
- A map cannot contain itself as key, but as value. However, the equals and hashcode are no longer well-defined in such map.
Sub-interfaces of Map interface:
- SortedMap: All keys must implement the Comparable interface.
- NavigableMap: extends SortedMap<K,V>. provides navigation methods (lowerKey/Entry, floorKey/Entry, ceilingKey/Entry, and higherKey/Entry) returning closest matches for given search targets.
- ConcurrentMap: offer thread safety and atomicity
- ConcurrentNavigableMap: offer thread safety and atomicity
Implementations of Map Interface:
- HashMap: Hash table based implementation of the Map interface. It's equivalent to the obsolete implementation java.util.Hashtable, except that HashMap accepts null keys and values, and it is unsynchronized. No guarantee on the constant order over time. O(1) for put/get.
- initial capacity: Default = 16 (number of "buckets")
- load factor: A measure of how full the hash table is allowed to get before its capacity is automatically increased. (default = 0.75) The lower, more free buckets, less collision, lower time complexity.
- LinkedHashMap: (extends HashMap)
- TreeMap: implements NavigableMap


No comments:
Post a Comment