Set is a interface it extends Collection interface.
- Set doesn’t allow duplicate elements.
- Insertion order is not preserved in Set.
- null insertion is possible but only once.
Set interface implemented by the AbstractSet, HashSet, LinkedHashSet, TreeSet, EnumSet.
Vector is a class which extends AbstractList and implements List interface.
- Vector is a Legacy collections class. Which is introduced in Java 1.0.
- Vector is synchronized, so it can be used in multi threading.
- Vector implements Random access interface so we can access the elements from the vector randomly.
- Initial capacity of vector is “10” . When it reaches to max then capacity will be increased to double.
- All vector specific methods are synchronized.
- Vector(): Creates empty Vector with initial capacity “10”.
- Vector(int capacity): Creates empty vector with specified capacity.
- Vector(int capacity, int incrementalCapacity): Creates empty vector with specified capacity and when it reaches to max capacity then add the incrementalCapacity.newCapacity = oldCapacity + incrementalCapacity ;
- Vector(Collection c): It creates the Vector which will contain all the elements of the specified Collection.
LinkedList is a collection class which extends AbstractList and implements List interface.
- LinkedList underlying data structure is doubly Linkedlist.
- It doesn’t implements Random access interface so we can’t access the elements randomly.
- It is not synchronized. If we want to use LinkedList with multi threading then we need to synchronized explicitly using Collections.synchronizedList(List l).
- Accessing elements with LinkedList is heavy process because if we want to access a element it needs to traverse full list if the element in the last position.
- Adding and removing elements is lightweight process in LinkedList, because it just modify the address of a node.
- LinkedList requires more memory than ArrayList because each node holds previous element address, data and next element address.
ArrayList is a Collection class which extends AbstractList and implements List interface.
- ArrayList underlying data structure is dynamic grow-able array.
- It implements random access interface. So we can access elements from the array list randomly by using index.
- ArrayList is not synchronized. So if we are using ArrayList in multi threading we need to explicitly make ArrayList as synchronized by using Collection.synchronizedList(List l) method.
- In ArrayList we can’t be used with primitives with generic.(ArrayList a = new ArrayList<>() : It is invalid and compile time error).
- ArrayList(): Creates the object with default initial capacity. (Till Java 1.6 initial capacity is “10” but from java 1.7 initial capacity is “0” but when we add first element then capacity will be “10”).
- ArrayList(int initialCapacity): creates the object with specified capacity.
- ArrayList(Collection c): Creates the object with specified collection data.
When the capacity increased to max then the new capacity will be
int newCapacity = (oldCapacity * 3)/2;
List is a interface which extends Collection interface.
- In List duplicates are allowed and insertion order is preserved.
- List is index based so we can access, remove and add elements based on index.
- List allows multiple null insertions.
- List allows heterogeneous values
List interface is implemented by the ArrayList, LinkedList, Vector and Stack classes.
It has all the functionalities of Collection interface along with below functions.
- void add(index i, Object o): It is used for insert the element at the specified index.
- boolean addAll(index i, Collection c):Insert the collection in specified index.
- Object remove(index i): Remove the element from the specified index and returns the removed element.
- Object get(index i): Get the element from the specified index.
- Object set(index i, Object o): Replaces the element in the specified index and returns the old element.
- int indexOf(Object o): returns the index of first occurrence of given element. If element not found then returns -1.
- int lastIndexOf(Object 0): returns the index of last occurrence of given element.If element not found then returns -1.
- ListIterator listIterator(): it returns the iterator object.
- List subList(int minIndex, int maxIndex): Returns the sub list from the main list from mimIndex to maxIndex-1.
Collection Framework introduced in java 1.2 and its improved a log in the latest versions of Java. Collections are available in java when its started. those are Vector, Stack, HashTable and Array, these are called legacy collection classes.
Collection : Collection is a root interface for the collection framework and it represents a group of objects as single unit. Collection interface consists all the common methods which are used to manipulate the collection(Group of objects).
Collection interface Methods:
- public boolean add(Object element) -> Used for insert the elements in a collection.
- public boolean addAll(Collection c) -> Used for insert the collection in to a collection.
- public boolean remove(Object element) -> Used for remove the specified element from the collection.
- public boolean removeAll(Collection c) -> Used for remove the specified collection from a collection.
- public boolean retainAll(Collection c) -> Remove the elements from the collection except specified collection.
- public int size() -> It return the total number of elements in a collection.
- public void clear() -> Removes all the elements from a collection.
- public boolean contains(Object element) -> Used for search given element present in a collection or not.
- public boolean containsAll(Collection c) -> Used for search a specified collection in a collection.
- public Iterator iterator() -> It returns the Iterator object.
- public Object toArray() -> Converts the collection into a Array.
- public boolean isEmpty() -> Used for check the collection is empty or not.
- public boolean equals(Object element) -> Used to compare the collection with other collection.
- public in hashCode() -> It return hashcode of a collection.