0% found this document useful (0 votes)
20 views

Collections in Java

this ppt explains what are collections

Uploaded by

aaryansh2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Collections in Java

this ppt explains what are collections

Uploaded by

aaryansh2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Collections in Java

• The Collection in Java is a framework that provides an


architecture to store and manipulate the group of
objects.
• Java Collections can achieve all the operations that you
perform on a data such as searching, sorting, insertion,
manipulation, and deletion.
• Java Collection means a single unit of objects. Java
Collection framework provides many interfaces (Set,
List, Queue, Deque) and classes (ArrayList, Vector,
LinkedList, PriorityQueue, HashSet, LinkedHashSet,
TreeSet).
• What is Collection in Java
A Collection represents a single unit of objects, i.e., a group.

• What is a framework in Java


It provides readymade architecture.
It represents a set of classes and interfaces.
List Interface
• List interface is the child interface of Collection interface. It
inhibits a list type data structure in which we can store the
ordered collection of objects. It can have duplicate values.
• List interface is implemented by the classes ArrayList,
LinkedList, Vector, and Stack.
• To instantiate the List interface, we must use :
1.List <data-type> list1= new ArrayList();
2.List <data-type> list2 = new LinkedList();
3.List <data-type> list3 = new Vector();
4.List <data-type> list4 = new Stack();
• There are various methods in List interface that can be used to
insert, delete, and access the elements from the list.
• The classes that implement the List interface are given below.
ArrayList
• The ArrayList class implements the List interface. It uses
a dynamic array to store the duplicate element of
different data types. The ArrayList class maintains the
insertion order and is non-synchronized. The elements
stored in the ArrayList class can be randomly accessed.
Consider the following example.
ArrayList
• It inherits the AbstractList class and implements List interface.
• The important points about the Java ArrayList class are:
• Java ArrayList class can contain duplicate elements.
• Java ArrayList class maintains insertion order.
• Java ArrayList class is non synchronized.
• Java ArrayList allows random access because the array works on an index
basis.
• ArrayList, manipulation is a little bit slower than the LinkedList in Java because
a lot of shifting needs to occur if any element is removed from the array list.
• We can not create an array list of the primitive types, such as int, float, char,
etc. It is required to use the required wrapper class in such cases. For example:

1. ArrayList<int> al = ArrayList<int>(); // does not work


2. ArrayList<Integer> al = new ArrayList<Integer>(); // works fine
Constructors of ArrayList

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>();

//Creating a List of type Integer using ArrayList


List<Integer> list=new ArrayList<Integer>();

//Creating a List of type Book using ArrayList


List<Book> list=new ArrayList<Book>();

//Creating a List of type String using LinkedList


List<String> list=new LinkedList<String>();
LinkedList
• LinkedList implements the Collection interface. It uses a doubly linked
list internally to store the elements. It can store the duplicate
elements. It maintains the insertion order and is not synchronized. In
LinkedList, the manipulation is fast because no shifting is required.
• The important points about Java LinkedList are:

 Java LinkedList class can contain duplicate elements.


 Java LinkedList class maintains insertion order.
 Java LinkedList class is non synchronized.
 In Java LinkedList class, manipulation is fast because no shifting needs to occur.

• 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

• The stack is the subclass of Vector. It implements the


last-in-first-out data structure, i.e., Stack. The stack
contains all of the methods of Vector class and also
provides its methods like boolean push(), boolean
peek(), boolean push(object o), which defines its
properties.
Java HashSet

• 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

• Queue interface maintains the first-in-first-out order. It


can be defined as an ordered list that is used to hold the
elements which are about to be processed. There are
various classes like PriorityQueue, Deque, and
ArrayDeque which implements the Queue interface.
• Queue interface can be instantiated as:
1.Queue<String> q1 = new PriorityQueue();
2.Queue<String> q2 = new ArrayDeque();

You might also like