In this post, we will show you how to use the Lists.newLinkedList() method in Java, and explain why it is useful and how it works. We will also compare it with other ways of creating linked lists in Java, and highlight some of its advantages and limitations.

Linked lists are collections of elements that are linked by pointers. They can offer some advantages over arrays or lists, such as constant-time insertion and deletion at the beginning or the end, without resizing the underlying data structure. However, creating linked lists in Java can be tedious and verbose, especially if you want to create them from existing collections or iterables. That’s where Guava’s Lists.newLinkedList() method comes in handy.

1. Overview of Lists.newLinkedList() method

The Lists.newLinkedList() method creates and returns a LinkedList instance, which is an implementation of the List interface that uses a doubly-linked list as its internal data structure. It has two overloaded versions:

  • Lists.newLinkedList(): This version creates an empty linked list. However, this method is unnecessary and should be treated as deprecated in Java 7 and later, as the diamond operator allows you to create an empty LinkedList with less verbosity.
  • Lists.newLinkedList(Iterable<? extends E> elements): This version creates a linked list containing the elements from the given iterable. If the iterable is a Collection, you don’t need this method in Java 7 and later, as you can use the LinkedList constructor directly with diamond operator.

To use the Lists.newLinkedList() method, you need to provide an optional iterable that contains the elements that you want to include in the linked list. The iterable can be any type that implements the Iterable interface, such as List, Set, Queue, Stream, etc. The elements can be any objects, including nulls.

For example, suppose you want to create a linked list that contains some information about countries and their capitals. You can use either an empty or a non-empty iterable as input:

Download Code

 
Once you have created a linked list using the Lists.newLinkedList() method, you can use it as any other type of List or Deque. For example:

Download Code

2. Advantages and Limitations of using the Lists.newLinkedList() method

Using the Lists.newLinkedList() method has many advantages, such as:

  • It is concise and expressive. You can create a linked list with zero or more elements in one line of code, without having to use the new keyword or specify the generic type parameter.
  • It is consistent and convenient. You can use the same method to create a linked list from any type of iterable, such as a collection, an array, or a stream. You don’t have to worry about converting the iterable to an array or a collection first.
  • It is flexible and interoperable. You can use the returned LinkedList instance as any type of List or Deque, depending on your needs. You can also easily convert it to other types of collections or iterables using Guava’s utility methods.

However, using the Lists.newLinkedList() method also has some limitations, such as:

  • To use this method, you need Guava as a dependency. You may have to add it as a dependency if your project does not already use Guava.
  • It may not be compatible with other frameworks or libraries. Some frameworks or libraries may expect or return other types of lists instead of linked lists, and may not work well with this method. You may need to convert between different types of lists when using them.

3. Other ways of creating Linked Lists in Java

There are other ways of creating linked lists in Java, such as using the LinkedList class constructor or the Collections class methods. How do they compare with using the Lists.newLinkedList() method? Here are some differences:

The LinkedList class constructor creates and returns a LinkedList instance. It has two overloaded versions:

  • LinkedList(): This version creates an empty linked list.
  • LinkedList(Collection<? extends E> c): This version creates a linked list containing the elements from the given collection.

For example:

Download Code

 
Compared to the Lists.newLinkedList() method, the LinkedList class constructor has some disadvantages, such as:

  • It is more verbose and less expressive. You have to use the new keyword and specify the generic type parameter.
  • It is less consistent and convenient. You can only use a collection as the input, not any type of iterable. You have to convert the iterable to a collection first, which may be inefficient or complex.

 
The Collections class methods provide some static factory methods for creating unmodifiable or singleton linked lists, such as:

  • Collections.unmodifiableList(List<? extends T> list): This method returns an unmodifiable view of the given list.
  • Collections.singletonList(T o): This method returns an immutable list containing only the specified object.

For example:

Download Code

 
Compared to the Lists.newLinkedList() method, the Collections class methods have some disadvantages, such as:

  • They do not create new linked lists. They only return views or wrappers of existing lists.
  • They do not allow modification of the lists. Any attempt to do so will result in an UnsupportedOperationException.

4. Conclusion

In this post, we have shown you how to use Guava’s Lists.newLinkedList() method in Java, and explained why it is useful and how it works. We have also compared it with other ways of creating linked lists in Java, and highlighted some of its advantages and limitations.

If you are interested in learning more about this method, you can check out the Guava official website or its GitHub repository.