How to create a Queue from LinkedList in Java?

Java 8Object Oriented ProgrammingProgramming

Let us create a queue from LinkedList like this −

Queue<String>queue = new LinkedList<String>();
queue.add("P");
queue.add("Q");
queue.add("R");
queue.add("S");
queue.add("T");
queue.add("U");
queue.add("V");

Now, use a List to display the elements of the queue −

List<String>list = new ArrayList<String>(queue);

Example

 Live Demo

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Demo {
   public static void main(String[] args) {
      Queue<String>queue = new LinkedList<String>();
      queue.add("P");
      queue.add("Q");
      queue.add("R");
      queue.add("S");
      queue.add("T");
      queue.add("U");
      queue.add("V");
      List<String>list = new ArrayList<String>(queue);
      for (Object ob: list)
         System.out.println(ob);
   }
}

Output

P
Q
R
S
T
U
V
raja
Published on 24-Apr-2019 09:12:08
Advertisements