Skip to content

Java LinkedList Tutorial with Examples

Java LinkedList tutorial with examples will help you understand how to use Java LinkedList in an easy way. LinkedList in Java is a doubly linked list implementation of the List and Deque interfaces. The LinkedList class implements all the optional operations defined by the List and allows all elements to be added to it including null elements.

The LinkedList in Java behaves exactly like what is expected from the doubly linked list.

Java LinkedList Tutorial

Get operation using the index will fetch the element either from the start of the linked list or from the end of the linked list depending upon which is closer to the specified index.

The LinkedList in Java is not a synchronized implementation. That means if there are multiple threads trying to modify the LinkedList object simultaneously, the code must take care of the synchronization explicitly. That can be done either by synchronizing the linked list object or getting the synchronized list object from the existing LinkedList object using synchronizedList method of the Collections class as given below.

The Iterator and ListIterator objects can be obtained from the LinkedList in order to iterate over the elements of the LinkedList. The Iterator and ListIterator objects returned by the LinkedList class are fail-fast in nature. That means if the LinkedList object is modified structurally (except through add or remove methods of the Iterator) after the Iterator or ListIterator is obtained from the LinkedList, it will throw ConcurrentModificationException.

Below given are some of the important methods of the LinkedList class.

How to add elements to the LinkedList?

The LinkedList class provides several methods to add elements to the LinkedList object as given below.

How to add an element to LinkedList using the add method?

The add method appends the given element at the end of the linked list. This method returns true if the element is appended at the end of the LinkedList.

Output

How to insert an element at the specified index of the LinkedList using the add method?

The overloaded add method inserts the given element at the specified index of the LinkedList object.

Output

How to add an element at the beginning of the LinkedList using the addFirst method?

The addFirst method adds an element at the beginning of the LinkedList object (at the front of the LinkedList or at the start of the LinkedList).

Output

You can also use the add method with the index as 0 to add an element at the front of the LinkedList instead of using addFirst method.

How to append an element at the end of the LinkedList using the addLast method?

The addLast method appends an element at the end of the LinkedList object. This method is similar to the add method as both the methods add the specified element at the end.

Output

How to add all elements of another list to LinkedList using the addAll method?

The addAll method of the LinkedList class appends all elements of the specified Collection to the LinkedList at the end.

Output

The above method adds all the elements of the Collection at the end of the LinkedList. If you want to insert all the elements at the given index instead of at the end, use the overloaded addAll method with the index parameter.

Output

How to get elements from the LinkedList?

There are several ways to get the elements from the LinkedList.

How to get an element from LinkedList the get method?

To get an element at the specified index from the LinkedList, use the get method.

Output

How to get first and last elements from LinkedList using the getFirst and getLast methods?

To get the first element from the LinkedList, use the getFirst method. Similarly, use the getLast method to get the last element of the LinkedList.

Output

How to get first and last elements from LinkedList using the peek, peekFirst and peekLast methods?

Instead of using getFirst and getLast methods, you can also use peek, peekFirst and peekLast methods to get respective elements from the LinkedList.

Output

How to remove elements from the LinkedList?

There are several ways to remove elements from the LinkedList.

How to remove an element at the specified index of LinkedList using the remove method?

To remove an element from the LinkedList at the specified index, use the remove method with an index argument. The remove method returns the value which was removed from the LinkedList.

Output

How to remove first and last elements from LinkedList using the removeFirst and removeLast methods?

You can use the remove method without any arguments to remove the first element from the LinkedList. This method throws NoSuchElementException if the linked list is empty. Similarly, you can use removeFirst and removeLast methods to remove the first and the last element from the linked list respectively.

Output

You can also use poll, pollFirst and pollLast methods instead of remove, removeFirst and removeLast methods.

How to remove a specified element from the LinkedList without using the index?

Instead of specifying an index, you can also specify the object or an element you want to remove. To do that, use the remove method with the Object parameter.

Output

You can also use the removeFirstOccurrence method to remove the first occurrence of the specified object in the linked list.

To remove the last occurrence of the specified object, use the removeLastOccurrence method.

Output

How to remove all elements from the LinkedList using the clear method?

The clear method of the LinkedList class removes all the elements from the linked list object.

Output

How to get the size of the LinkedList using the size method?

To get the number of elements stored in the LinkedList, use the size method.

Output

How to check if the LinkedList is empty using the isEmpty method?

The isEmpty method of the LinkedList returns true if the linked list is empty (i.e. there are no elements in the linked list). It returns false if there is at least one element in the LinkedList. Alternatively, you can also get the linked list size using the size method and compare it with zero to check if the linked list is empty.

Output

How to replace an element in the LinkedList using the set method?

The set method replaces an element with the given element at the specified index of the linked list. The set method returns the element which was replaced by the specified new element at the given index.

Output

How to convert LinkedList to an array using the toArray method?

The toArray method of the LinkedList class converts the linked list object to an array. The array contains all the elements of the LinkedList in the same order.

Output

How to iterate LinkedList?

There are several ways using which you can iterate the linked list object.

How to iterate LinkedList using the Iterator?

Output

How to iterate LinkedList using ListIterator?

Output

Additionally, you can also iterate the linked list in the reverse direction using a ListIterator object.

Output

How to iterate LinkedList using enhanced for loop?

Output

How to iterate LinkedList in reverse direction using the descendingIterator method?

The descendingIterator method returns an iterator over the elements of the linked list in the reverse order (i.e. starting from the end to the start of the linked list).

Output

How to get a sublist from LinkedList using the subList method?

The subList method returns a portion of the LinkedList between the specified start and end index. The start index is inclusive while the end index is exclusive. The returned List object is backed by the original linked list, so any changes you make to the sublist will also be reflected back to the linked list, and vice versa.

Output

The below given additional Java LinkedList examples will help you understand the linked list in more detail.

Java LinkedList Examples

References:
Java 8 LinkedList

Please let me know if you liked the Java LinkedList tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.