Unit 4 - Collection Framework
Unit 4 - Collection Framework
1
Collection in Java,
Collection Framework in Java,
Hierarchy of Collection Framework,
Iterator Interface,
Collection Interface,
List Interface, ArrayList, LinkedList, Vector, Stack, Queue Interface,
Set Interface, HashSet, LinkedHashSet,
SortedSet Interface, TreeSet,
Map Interface, HashMap Class,
LinkedHashMap Class,
TreeMap Class,
Hashtable Class, Sorting,
Comparable Interface, Comparator Interface,
Properties Class in Java.
Collection Interface
A Map doesn't allow duplicate keys, but you can have duplicate values.
HashMap and LinkedHashMap allow null keys and values, but TreeMap
doesn't allow any null key or value.
General Properties of Map
• Key-Value Pairs:
– A Map stores data in key-value pairs. Each key is
associated with exactly one value.
• Unique Keys:
– Keys in a Map are unique. If you try to insert a duplicate
key, the old value associated with that key will be replaced
by the new value.
• Null Values and Keys:
– HashMap allows one null key and multiple null values.
– TreeMap does not allow null keys as it needs to sort the
keys, but it allows multiple null values.
– LinkedHashMap allows one null key and multiple null
values.
Order:
• HashMap does not guarantee any specific order of the keys.
• LinkedHashMap maintains the insertion order of keys.
• TreeMap maintains keys in a sorted order according to their
natural ordering or a specified comparator.
Performance:
• HashMap provides O(1) time complexity for put, get,
and remove operations on average.
• TreeMap provides O(log n) time complexity for put,
get, and remove operations as it is based on a
Red-Black tree.
• LinkedHashMap provides O(1) time complexity for
put, get, and remove operations, similar to
HashMap, but with a slightly higher overhead due
to maintaining the insertion order.
Concurrency:
• HashMap and TreeMap are not synchronized and are
not thread-safe.
HashMap
Example: An application that frequently needs to cache data for quick access.
Reason: HashMap provides average O(1) time complexity for get and put operations,
making it ideal for caching scenarios where quick access to data is critical.
TreeMap
Use Case: Storing Sorted Data
Example: An application that needs to maintain a directory of employees sorted by
their employee IDs.
Reason: TreeMap maintains the keys in a sorted order, making it suitable for scenarios
where sorted data retrieval is required.
LinkedHashMap
Use Case: Maintaining Insertion Order
Example: An application that needs to maintain the order of user actions for session
tracking.
Reason: LinkedHashMap maintains the insertion order, which is useful for preserving
the sequence of actions or events.
HashTable
Features of Hashtable
1. It is similar to HashMap, but is synchronized.
2. The initial default capacity of Hashtable class is 11.
3. Hashtable stores key/value pair in hash table.
4. In Hashtable we specify an object that is used as a key, and the value we
want to associate to that key. The key is then hashed, and the resulting
hash code is used as the index at which the value is stored within the table
5. Load factor =.75 (it is used at the time of resizing). The Hashtable has a
default load factor of 0.75.
6. When the number of entries exceeds 75% of the capacity, the Hashtable
rehashes and doubles its capacity plus one. This means if the capacity is
C, after rehashing, the new capacity will be
2*C + 1.
1. HashTable output is Top to Bottom and Right to left.
Differences between HashMap and HashTable