Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn how to convert a Queue object into a List in Java.

We’ll explain several popular ways of doing this along with their implementations, and we’ll end each section with a test case to test the respective implementation.

2. Ways to Convert Queue to List

In this section, we’ll cover different ways to convert the Queue to a List using standard Java classes and methods. We’re assuming a non-null queue for all examples.

2.1. Using the ArrayList Constructor

The ArrayList constructor provides the easiest and most common way to convert a Queue to an ArrayList.

The idea is to pass the Queue as a parameter to the ArrayList constructor:

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

The constructor new ArrayList<>(queue) efficiently inserts all of the queue elements into the new ArrayList.

2.2. Using the addAll() Method

The addAll() method is another great option to consider if we want to convert a Queue to a List.

As the name implies, this method allows adding all elements in the specified collection to the List.

Now, let’s see the logic:

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

We create a new List named list and populate it with the elements of the queue using the addAll() method. The method then returns the resultant list.

2.3. Using the LinkedList Constructor

The LinkedList constructor provides the most common and easiest way to convert a Queue to a LinkedList.

Similar to the above example using the ArrayList constructor, we simply pass the Queue as a parameter to the LinkedList constructor:

LinkedList<String> list = new LinkedList<>(queue);

The constructor new LinkedList<>(queue) efficiently converts the queue elements into the new LinkedList.

2.4. Using Stream API

Java 8 comes with a lot of new features that help in enhancing our code. Among these features, we find the Stream API.

Let’s illustrate how to use the stream API to convert a Queue to a List:

List<String> list = queue.stream().collect(Collectors.toList());

We convert the queue to a List using the collect(Collectors.toList()) operation, which collects the elements of the stream into a new list and returns it. This approach leverages a concise and functional programming style with streams to perform the conversion.

2.5. Using Guava

Guava is a popular open-source library developed by Google that provides a wide range of utility classes and methods to simplify common programming tasks in Java.

Let’s use Guava to convert Queue to List:

List<String> list = Lists.newArrayList(queue);

Guava’s utility method Lists.newArrayList(queue) simplifies the process of creating a new list from the elements of a queue.

3. Conclusion

In this article, we saw various ways to convert Queue to List in Java. Whether we prefer the simplicity of LinkedList, the versatility of ArrayList, the elegance of Java 8 streams, or the power of Guava, understanding these techniques empowers us to handle data seamlessly in our Java projects. We can experiment with these approaches to find the one best suited to our requirements and coding style.

The source code of all these examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments