PriorityQueue And ArrayDeque in Java

             Queue is a kind of data structure which follows First In First Out (FIFO), which means the element which is inserted first will be removed first.

To implement Queue data structure, Java collections framework provides PriorityQueue and ArrayDeque classes which implements Queue interface. 

Let's learn about these classes in detail:

1. PriorityQueue:

            It stores the elements or objects based on their priority, which means the elements are ordered according to their natural ordering.

Note:  In PriorityQueue, we cannot store a null value.
         - By default, it follows min Heap.

Syntax:

            Queue<E> arr = new PriorityQueue<E>(); 
            OR
             PriorityQueue<E> arr = new PriorityQueue<E>();

Useful Methods of ArrayList
                PriorityQueue<Integer> arr = new PriorityQueue<>();
  • add(element): adds element in the priority queue
               Example:
                            arr.add(12);
                            arr.add(2);
                            arr.add(5);
                            System.out.println(arr); //output: [2,12, 5]
  • peek(): retrieves the head element of the queue if exists otherwise returns null.
                            System.out.println(arr.peek());  //output: 2
  • poll( ): retrieves and removes the head element of the queue if exists othewise return null if queue is empty
                             System.out.println(arr.poll()); //output: 2
                             System.out.println(arr); //output: [5, 12]
  • remove(Object o): removes the single instance of the object from the queue if exists
                            arr.remove(12);  //removes element 12
                            System.out.println(arr); //[5]
  • size(): returns the number of elements present in the queue
                            System.out.println(arr.size()); //returns 1, since queue contains 1 elements i.e. 5
  • clear(): removes all the elements from the queue
                            arr.clear();
                            System.out.println(arr); //output: [ ]

2. ArrayDeque:
            ArrayDeque implements Deque interface( which extends Queue interface ). And allows us to perform insert and delete operation from both the ends of the queue.

Syntax:
            Deque<E> variable = new ArrayDeque<E>();
            OR
            ArrayDeque<E> variable = new ArrayDeque<E>();

       Let's understand some useful methods of ArrayDeque:
                ArrayDeque<String> names = new ArrayDeque<>();

       All the above methods of PriorityQueue can also be applied on ArrayDeque, some additional methods of ArrayDeque is defined below:

  • addFirst(element): adds the element at the front of the arraydeque
  • addLast(element): add the element at the end of the arraydeque
  • getFirst(): returns the first element of the arraydeque if exists
  • getLast(): returns the last element of the arraydeque if exists
  • peekFirst(): returns the first element of the arraydeque if exists, otherwise returns null
  • peekLast(): returns the last element of the arraydeque if exists, otherwise returns null
  • pollFirst(): returns and removes the first element of the arraydeque if exists, otherwise returns null
  • pollLast(): returns and removes the last element of the arraydeque if exists, otherwise returns null


Continue Learning: HashSet, LinkedHashSet And TreeSet















Comments

Popular Posts