Collections in Java
Collections in Java
Constructor Description
ArrayList() It is used to build an
empty array list.
ArrayList(Collection<? It is used to build an array
extends E> c) list that is initialized with
the elements of the
collection c.
ArrayList(int capacity) It is used to build an array
list that has the specified
initial capacity.
//Creating a List of type String using ArrayList
List<String> list=new ArrayList<String>();
• Vector
Vector uses a dynamic array to store the data elements. It is similar to
ArrayList. However, It is synchronized and contains many methods that
are not the part of Collection framework.
Difference Between ArrayList and
LinkedList
ArrayList LinkedList
1) ArrayList internally uses a dynamic array to store LinkedList internally uses a doubly linked list to store
the elements. the elements.
2) Manipulation with ArrayList is slow because it Manipulation with LinkedList is faster than ArrayList
internally uses an array. If any element is removed from because it uses a doubly linked list, so no bit shifting is
the array, all the other elements are shifted in memory. required in memory.
3) An ArrayList class can act as a list only because it LinkedList class can act as a list and queue both
implements List only. because it implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
5) The memory location for the elements of an ArrayList The location for the elements of a linked list is not
is contiguous. contagious.
6) Generally, when an ArrayList is initialized, a default There is no case of default capacity in a LinkedList. In
capacity of 10 is assigned to the ArrayList. LinkedList, an empty list is created when a LinkedList is
initialized.
7) To be precise, an ArrayList is a resizable array. LinkedList implements the doubly linked list of the list
interface.
• The following are some important points to remember regarding an
ArrayList and LinkedList.
When the rate of addition or removal rate is more than the read scenarios,
then go for the LinkedList. On the other hand, when the frequency of the
read scenarios is more than the addition or removal rate, then ArrayList
takes precedence over LinkedList.
Since the elements of an ArrayList are stored more compact as compared to
a LinkedList; therefore, the ArrayList is more cache-friendly as compared to
the LinkedList. Thus, chances for the cache miss are less in an ArrayList as
compared to a LinkedList. Generally, it is considered that a LinkedList is
poor in cache-locality.
Memory overhead in the LinkedList is more as compared to the ArrayList. It
is because, in a LinkedList, we have two extra links (next and previous) as it
is required to store the address of the previous and the next nodes, and
these links consume extra space. Such links are not present in an ArrayList.
Stack
• Java HashSet class is used to create a collection that uses a hash table for
storage. It inherits the AbstractSet class and implements Set interface.
• The important points about Java HashSet class are:
• HashSet stores the elements by using a mechanism called hashing.
• HashSet contains unique elements only.
• HashSet allows null value.
• HashSet class is non synchronized.
• HashSet doesn't maintain the insertion order. Here, elements are inserted
on the basis of their hashcode.
• HashSet is the best approach for search operations.
• A list can contain duplicate elements whereas Set contains
unique elements only.
Queue Interface