What is Queue Interface?
Queue q2 = new PriorityQueue();
Please note that both the implementations are not thread safe. PriorityBlockingQueue is one alternative implementation if you need a thread safe implementation.
Methods of Queue interface:
1) boolean add(E e): This method adds the specified element at the end of Queue. Returns true if the the element is added successfully or false if the element is not added that basically happens when the Queue is at its max capacity and cannot take any more elements.
2) E element(): This method returns the head (the first element) of the Queue.
boolean offer(object): This is same as add() method.
3) E remove(): This method removes the head(first element) of the Queue and returns its value.
4) E poll(): This method is almost same as remove() method. The only difference between poll() and remove() is that poll() method returns null if the Queue is empty.
5) E peek(): This method is almost same as element() method. The only difference between peek() and element() is that peek() method returns null if the Queue is empty.
Java Queue Example:
1) Add elements in a queue
Output:
Elements in Queue:[Rick, Geoff, Glenn, Ken, Andrew]
Removed element: Rick
Head: Geoff
poll(): Geoff
peek(): Glenn
Elements in Queue:[Glenn, Ken, Andrew]
2) User-defined class objects in Java HashSet
- The elements added to it are placed at the end of Queue and removed from the beginning of Queue i.e in FIFO First in first out manner.
- Queue interface in Java collections has two implementation: LinkedList and PriorityQueue, these two classes implements Queue interface.
- Java Queue is an interface available in java.util package and extends java.util.Collection interface.
- Just like Java List, Java Queue is a collection of ordered elements (Or objects) but it performs insert and remove operations differently. We can use Queue to store elements before processing those elements.
- Queue is an interface so we cannot instantiate it, rather we create instance of LinkedList or PriorityQueue and assign it to the Queue like this:
Queue q2 = new PriorityQueue();
Please note that both the implementations are not thread safe. PriorityBlockingQueue is one alternative implementation if you need a thread safe implementation.
Methods of Queue interface:
1) boolean add(E e): This method adds the specified element at the end of Queue. Returns true if the the element is added successfully or false if the element is not added that basically happens when the Queue is at its max capacity and cannot take any more elements.
2) E element(): This method returns the head (the first element) of the Queue.
boolean offer(object): This is same as add() method.
3) E remove(): This method removes the head(first element) of the Queue and returns its value.
4) E poll(): This method is almost same as remove() method. The only difference between poll() and remove() is that poll() method returns null if the Queue is empty.
5) E peek(): This method is almost same as element() method. The only difference between peek() and element() is that peek() method returns null if the Queue is empty.
Java Queue Example:
1) Add elements in a queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import java.util.*; public class QueueDemo { public static void main(String[] args) { /* * * We cannot create instance of a Queue as it is an * * interface, we can create instance of LinkedList or * * PriorityQueue and assign it to Queue * */ Queue<String> q = new LinkedList<String>(); // Adding elements to the Queue q.add("Rick"); q.add("Geoff"); q.add("Glenn"); q.add("Ken"); q.add("Andrew"); System.out.println("Elements in Queue:" + q); /* * * We can remove element from Queue using remove() method, * * this would remove the first element from the Queue * */ System.out.println("Removed element: " + q.remove()); /* * * element() method - this returns the head of the * * Queue. Head is the first element of Queue * */ System.out.println("Head: " + q.element()); /* * * poll() method - this removes and returns the * * head of the Queue. Returns null if the Queue is empty * */ System.out.println("poll(): " + q.poll()); /* * * peek() method - it works same as element() method, * * however it returns null if the Queue is empty * */ System.out.println("peek(): " + q.peek()); // Again displaying the elements of Queue System.out.println("Elements in Queue:" + q); } } |
Elements in Queue:[Rick, Geoff, Glenn, Ken, Andrew]
Removed element: Rick
Head: Geoff
poll(): Geoff
peek(): Glenn
Elements in Queue:[Glenn, Ken, Andrew]
2) User-defined class objects in Java HashSet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Book implements Comparable<Book> { String name, author, publisher; int id, quantity; public Book(int id, String name, String author, String publisher, int quantity) { this.id = id; this.name = name; this.author = author; this.publisher = publisher; this.quantity = quantity; } @Override public int compareTo(Book b) { if (id > b.id) { return 1; } else if (id < b.id) { return -1; } else { return 0; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.PriorityQueue; import java.util.Queue; public class PriorityQueueExample { public static void main(String[] args) { Queue<Book> queue = new PriorityQueue<Book>(); // Creating Books Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8); Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6); Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4); // Adding Books to the queue queue.add(b1); queue.add(b2); queue.add(b3); System.out.println("Traversing the queue elements:"); // Traversing queue elements for (Book b : queue) { System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity); } queue.remove(); System.out.println("\nAfter removing one book record:"); for (Book b : queue) { System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity); } } } |
No comments:
Post a Comment