Queue and Set Interface

Queue Interface:

Java Queue interface orders the element in FIFO(First In First Out) manner. In FIFO, first element is removed first and last element is removed at last.

How to create the objects?

Queue<String> q1 = new PriorityQueue();
Queue<String> q2 = new ArrayDeque();

PriorityQueue Class

Properties: It holds the elements or objects which are to be processed by their priorities. PriorityQueue doesn't allow null values to be stored in the queue.

Methods: boolean add(object), boolean offer(object), boolean remove(object), Object poll(), Object element() , Object peek(), void clear(), int size()

Deque Interface:

Deque interface extends the Queue interface. In Deque, we can remove and add the elements from both the side. Deque stands for a double-ended queue which enables us to perform the operations at both the 

Deque d = new ArrayDeque();


ArrayDeque Class

ArrayDeque class implements the Deque interface. It facilitates us to use the Deque. Unlike queue, we can add or delete the elements from both the ends.

ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.

Set Interface

Properties: 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.

Set<data-type> s1 = new HashSet<data-type>();  
Set<data-type> s2 = new LinkedHashSet<data-type>();  
Set<data-type> s3 = new TreeSet<data-type>(); 

HashSet Class

Properties: This class implements the Set interface.
--HashSet doesn’t maintain any order,
--HashSet doesn’t allow duplicates
--HashSet allows null values however if you insert more than one nulls it would still return only one null value.
--HashSet is non-synchronized.
--Hashing is used to store the elements in the HashSet

HashSet<String> hset = new HashSet<String>();

Methods: boolean add(E e) , void clear(), Object clone(), boolean contains(Object o), boolean isEmpty(), int size().

LinkedHashSet Class

Properties: This class implements the Set interface.
--LinkedHashSet maintains insertion order,
--LinkedHashSet doesn’t allow duplicates
--LinkedHashSet allows null values however if you insert more than one nulls it would still return only one null value.
--LinkedHashSet is non-synchronized.
LinkedHashSet<String> set=new LinkedHashSet<String>(); 

Methods: boolean add(E e) , void clear(), Object clone(), boolean contains(Object o), boolean isEmpty(), int size(), boolean remove(Object o), removeAll().


TreeSet Class

Properties: This class implements the SortedSet interface.
--The access and retrieval time of TreeSet is quite fast. The elements in TreeSet stored in ascending order.
--TreeSet doesn’t allow duplicates
--TreeSet doesn’t allows null values 
--TreeSet is non-synchronized.

TreeSet<String> tset = new TreeSet<String>();

Methods: boolean add(E e) , void clear(), Object clone(), boolean contains(Object o), boolean isEmpty(), int size(), boolean remove(Object o), Object first(), Object last(). 

To get more details please watch below youtube video and Subscribe the channel.




No comments:

Post a Comment