Collections KeyPoints
The Main Collections KeyPoints in java
Collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs.
Eg. Hashtable, HashMap, TreeMap, and LinkedHashMap (LinkedList+HashTable). Explained in detail in last post, how to iterate HashMap.
Here the collections of classes and interfaces having some their own properties.They are
1.Heterogenious Objects:
Collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs.
Eg. Hashtable, HashMap, TreeMap, and LinkedHashMap (LinkedList+HashTable). Explained in detail in last post, how to iterate HashMap.
Here the collections of classes and interfaces having some their own properties.They are
1.Heterogenious Objects:
- TreeSet and TreeMap will allow different types of objects as Integer,String,Long and user defined objects . etc.
- Given Classes can follow the insertion order:
- ArrayList, LinkedList, LinkedHashSet and LinkedHashMap -insertion order of keys (by default).
- Only List and its Child Classes will allow the duplicate objects. Ex: ArrayList, LinkedList and Vector.
- Only Map and its Child Classes, values will be allowed as duplicates and keys are not.
- Map allows only one null key and many null values as duplicates.
- If you given again the key,value it will be overriden in Map.
- Only TreeMap and TreeSet Classes will follow the Sorted order.
- These two classes implements SortedSet and SortedMap Interfaces.
- HashTable, TreeSet and TreeMap classes only rejects nulls.remaining will allow null.
- Vector, Stack, HashTable and Properties are Synchronized.
Breif Description on Collections and its Interfaces
Interface | Brief Description | Derived Classes |
---|---|---|
Collection | It forms the root interface and comes with methods that define basic operations and makes all DS as one unit (called by a common name) | Majority of all DS come under this interface |
Set | Subclasses of this interface have a common feature of not allowing duplicate elements | HashSet, LinkedHashSet |
SortedSet | Subclasses of this interface come with a common feature of printing the elements in ascending order implicitly. Being sub interface of Set, it allows only unique elements | TreeSet |
List | Subclasses allow duplicate elements | LinkedList, ArrayList, Vector |
Map | Allows the subclasses to store key/value pairs. Does not allow duplicate keys. | HashMap, LinkedHashMap, Hashtable |
SortedMap | Prints elements in ascending order of keys. Being a sub interface of Map, it inherits the property of not allowing the duplicate keys. | TreeMap |