Collections in Java
Java Collections API provide set of classes and interfaces to handle collection (or a group) of objects.
The java.util.List interface is a subtype of the java.util.Collection interface. It represents an ordered list of objects. The elements of list can be accessed in a specific order, and by index as well.
The java.util.Set interface is a subtype of the java.util.Collection interface. It represents set of non-repeatable objects.
The java.util.Queue interface is a subtype of the java.util.Collection interface. It represents an ordered list of objects just like a List, but its intended use is slightly different. A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue
The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface.
- LinkedList is implemented using doubly linked list, maintaining references to the previous and next element at each node in the list.
- It can be used both as List and Queue.
- LinkedList class can contain duplicate elements.
- It is non synchronized.The LinkedList in java can have any type of elements including null and duplicates.
- Elements can be inserted and can be removed from both the ends and can be retrieved from any arbitrary position.
- The class ArrayList provides resizable-array, i.e. items can be added and removed from the list.
- An ArrayList is a dynamic data structure, so it can be used when there is no upper bound on the number of elements.
- TreeSet sorts the elements in the ascending (unlike while HashSet doesn't maintain any order).
- TreeSet allows null element (HashSet doesn't).
- Like most of the other collection classes this class is also not synchronized (however it can be).
- Hash Map can be used to store key-value pairs.
- There cannot be duplicate keys in a Map and each key maps to at most one value.
- It can be used to store multiple values for the same key.
- HashMap is unsynchronized and Hashtable is synchronized. HashMap is the faster one between the two.
- To maintain map keys in an ordered fashion (asending orderof keys), one can use TreeMap.
- TreeMap is unsynchronized collection, so it is not suitable for thread-safe operations until unless synchronized explicitly.