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

The Collection in Java Is A Framewo

Uploaded by

Farook Shaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

The Collection in Java Is A Framewo

Uploaded by

Farook Shaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

Iterable
|
collection
| | | |
List Queue Set Map
|
ArrayList | -->PriorityQueue ->HashSet -->Hash
Map
|
LinkedList Deque ->LinkedHashSet -->
LinkedHashMap
|
Vector ->ArrayDeque | |
|
Stack SortedSet
SortedMap
| |
treeSet
treeMap

the Collection interface methods:


add(), addAll(), remove(), removeAll(), removeIf(), retainAll(), size(), clear(),
contains(), containsAll(),
equals(), hashcode()

Iterator Interface:
Iterator interface provide facility to iterate elements in forward direction only
hasNext(), next()

Java ArrayList class:


--------------------
1. Java ArrayList class can contain duplicate elements.
2. Java ArrayList class maintains insertion order.
3. Java ArrayList class is non synchronized.
4. Java ArrayList allows random access because the array works on an index basis.
5. In 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.
6. 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

Java LinkedList class:


---------------------
java LinkedList class uses a doubly linked list to store the elements. It provides
a linked-list data structure.

1. Java LinkedList class can contain duplicate elements.


2. Java LinkedList class maintains insertion order.
3. Java LinkedList class is non synchronized.
4. In Java LinkedList class, manipulation is fast because no shifting needs to
occur.
5. Java LinkedList class can be used as a list, stack or queue.
Java Vector class:
-----------------
Vector is like the dynamic array which can grow or shrink its size. Unlike array,
we can store n-number of elements in it as there is no size limit. It is a part of
Java Collection framework since Java 1.2. It is found in the java.util package and
implements the List interface, so we can use all the methods of List interface
here.

It is recommended to use the Vector class in the thread-safe implementation only.


If you don't need to use the thread-safe implementation, you should use the
ArrayList, the ArrayList will perform better in such case.
1. Java Vector class can contain duplicate elements.
2. Java Vector class maintains insertion order.
3. Java Vector class is synchronized.
4. Java Vector class has many legacy methods which are not a part of java
collection framework

Java Stack Class:


----------------
The stack is a linear data structure that is used to store the collection of
objects. It is based on Last-In-First-Out (LIFO). Java collection framework
provides many interfaces and classes to store the collection of objects. One of
them is the Stack class that provides different operations such as push, pop,
search, etc.
empty() boolean The method checks the stack is empty or not.
push(E item) E The method pushes (insert) an element onto the top of the
stack.
pop() E The method removes an element from the top of the stack and returns the
same element as the value of that function.
peek() E The method looks at the top element of the stack without removing
it.
search(Object o) int The method searches the specified object and returns the
position of the object.

Java Queue Interface:


--------------------
The interface Queue is available in the java.util package and does extend the
Collection interface. It is used to keep the elements that are processed in the
First In First Out (FIFO) manner. It is an ordered list of objects, where insertion
of elements occurs at the end of the list, and removal of elements occur at the
beginning of the list.'

PriorityQueue Class:
-------------------
PriorityQueue is also class that is defined in the collection framework that gives
us a way for processing the objects on the basis of priority. It is already
described that the insertion and deletion of objects follows FIFO pattern in the
Java queue. However, sometimes the elements of the queue are needed to be processed
according to the priority, that's where a PriorityQueue comes into action.

Java DeQue Interface:


--------------------
1. stack supports LIFO -->Last In First Out
2. queue supports FIFO -->First In First Out
Deque supports both LIFO and FIFO because it is double ended queue.

Java ArrayDeque class:


---------------------
We know that it is not possible to create an object of an interface in Java.
Therefore, for instantiation, we need a class that implements the Deque interface,
and that class is ArrayDeque. It grows and shrinks as per usage. It also inherits
the AbstractCollection class.

The important points about ArrayDeque class are:


1. Unlike Queue, we can add or remove elements from both sides.
2. Null elements are not allowed in the ArrayDeque.
3. ArrayDeque is not thread safe, in the absence of external synchronization.
4. ArrayDeque has no capacity restrictions.
5. ArrayDeque is faster than LinkedList and Stack.

Set Interface:
-------------
Set Interface in Java is present in java.util package. It extends the Collection
interface. It represents the unordered set of elements which doesn't allow us to
store the duplicate items. We can store at most one null value in Set. Set is
implemented by HashSet, LinkedHashSet, and TreeSet.

HashSet class:
-------------
1. HashSet stores the elements by using a mechanism called hashing.
2. HashSet contains unique elements only.
3. HashSet allows null value.
4. HashSet class is non synchronized.
5. HashSet doesn't maintain the insertion order. Here, elements are inserted on the
basis of their hashcode.
6. HashSet is the best approach for search operations.
The initial default capacity of HashSet is 16, and the load factor is 0.75.

Java LinkedHashSet Class


------------------------
1. Java LinkedHashSet class is a Hashtable and Linked list implementation of the
Set interface. It inherits the HashSet class and implements the Set interface.
2. Java LinkedHashSet class contains unique elements only like HashSet.
3. Java LinkedHashSet class provides all optional set operations and permits null
elements.
4. Java LinkedHashSet class is non-synchronized.
5. Java LinkedHashSet class maintains insertion order.

SortedSet Interface:
-------------------
SortedSet is the alternate of Set interface that provides a total ordering on its
elements. The elements of the SortedSet are arranged in the increasing (ascending)
order. The SortedSet provides the additional methods that inhibit the natural
ordering of the elements.

Java TreeSet class:


------------------
Java TreeSet class implements the Set interface that uses a tree for storage.
The objects of the TreeSet class are stored in ascending order.

1. Java TreeSet class contains unique elements only like HashSet.


2. Java TreeSet class access and retrieval times are quiet fast.
3. Java TreeSet class doesn't allow null element.
4. Java TreeSet class is non synchronized.
5. Java TreeSet class maintains ascending order.

Java Map Interface:


------------------
A map contains values on the basis of key, i.e. key and value pair. Each key and
value pair is known as an entry. A Map contains unique keys.
There are two interfaces for implementing Map in java: Map and SortedMap, and three
classes: HashMap, LinkedHashMap, and TreeMap. The hierarchy of Java Map is given
below:
A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and
LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or
value.

Java HashMap class:


------------------
HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It
allows us to store the null elements as well, but there should be only one null
key. Since Java 5, it is denoted as HashMap<K,V>, where K stands for key and V for
value. It inherits the AbstractMap class and implements the Map interface.
1. Java HashMap contains values based on the key.
2. Java HashMap contains only unique keys.
3. Java HashMap may have one null key and multiple null values.
4. Java HashMap is non synchronized.
5. Java HashMap maintains no order.

Java LinkedHashMap class:


------------------------
Java LinkedHashMap class is Hashtable and Linked list implementation of the Map
interface, with predictable iteration order.
1. Java LinkedHashMap contains values based on the key.
2. Java LinkedHashMap contains unique elements.
3. Java LinkedHashMap may have one null key and multiple null values.
4. Java LinkedHashMap is non synchronized.
5. Java LinkedHashMap maintains insertion order.

Java TreeMap class:


------------------
It provides an efficient means of storing key-value pairs in sorted order.
1. Java TreeMap contains values based on the key. It implements the NavigableMap
interface and extends AbstractMap class.
2. Java TreeMap contains only unique elements.
3. Java TreeMap cannot have a null key but can have multiple null values.
4. Java TreeMap is non synchronized.
5. Java TreeMap maintains ascending order.

Java Comparable interface:


--------------------------
ava Comparable interface is used to order the objects of the user-defined class.
This interface is found in java.lang package and contains only one method named
compareTo(Object). It provides a single sorting sequence only, i.e., you can sort
the elements on the basis of single data member only. For example, it may be
rollno, name, age or anything else.
public int compareTo(Object obj): It is used to compare the current object with the
specified object. It returns

positive integer, if the current object is greater than the specified object.
negative integer, if the current object is less than the specified object.
zero, if the current object is equal to the specified object.

Java Comparator interface:


--------------------------
Java Comparator interface is used to order the objects of a user-defined class.
This interface is found in java.util package and contains 2 methods compare(Object
obj1,Object obj2) and equals(Object element).

It provides multiple sorting sequences, i.e., you can sort the elements on the
basis of any data member, for example, rollno, name, age or anything else.

You might also like