How to get First and Last Element of LinkedList in Java [Example]

Hello guys, if you are wondering how to get the first and last element from Java LinkedList then you have come to the right place. Earlier, I have shared the best Data structure and algorithms courses and in today's post, I will teach you how to get the first and last element of a linked list in Java. This is one of the common coding problems but we will see it from different angles. In Java, you don't need to write and create your own LinkedList, there is a built-in class called java.util.LinkedIn and you can use that class whenever you need a linked list data structure. 

This class provides methods getFirst() and getLast() to retrieve the first and last element from a linked list. In this tutorial, I will show you how to use those methods to get the first and last node from a given linked list.

Instead of writing your own methods, you can use this class and these methods to retrieve the first and last objects in Java. As Joshua Bloch advises in his classic Effective Java book, "prefer library methods and open source libraries instead of writing your own". 

It not only saves time but is also logical because of the testing exposure these public libraries and APIs get, you won't be able to match them on the limited time you get for your feature development. More often than not, the open-source version of the method will perform better and will have fewer bugs than your own implementation, of course, there is the exception and sometimes it makes sense to have your proprietary libraries. 

How to get the first and last objects of a linked list in Java? Example 

The following example shows how to get the first and last element of a linked list with the help of LinkedList.getFirst() and LinkedList.getLast() of LinkedList class in Java. 

In this program, you can see that we have created a new LinkedList object and then added 5 String objects into it, which are nothing but numbers 100 to 500 in multiples of 100. Afte that we have used the getFirst() and getLast() method to retrieve the first and last element and printed them using System.out.println(), the oldest way to print something in Java. 

And, if you want to learn more about linked list data structure itself and learn how to create your own linked list in Java using object-oriented programming then you can also check out this Data Structure and Algorithms master class from Udemy to start with.

How to get first and last element of a linked list in Java

Anyway, here is our complete Java program which demonstrates how to use the LinkedList class for storing and retrieving objects.

import java.util.LinkedList;

public class Main {
   public static void main(String[] args) {
      LinkedList lList = new LinkedList();
 System.out.println("The first element of LinkedList is:
      " + lList.getFirst());
   System.out.println("The last element of LinkedList is:
      " + lList.getLast());

The above code sample will produce the following result.
The first element of LinkedList is:100
The last element of LinkedList is:500

You can see that,  we have first added 5 numbers from 100 to 500 into then LinkedList and then use the getFirst() and getLast() method to get the first and last element. The code return 100 and 500 which is as per the expected result. I have printed the output but you can also write JUnit test cases with an assert statement to check if the returned value matches with the expected value or not.

That's all about how to get the first and last element from a given LinkedList in Java. This one was a simple example but you learned how to use the LinkedList data structure in Java. If you guys like this kind of to-the-point article explaining simple methods and concepts of Java then tell us in the comments and I will write more of this. As always, your feedback is very important to us to produce better tutorials and guides. 

Other Data Structure  and Algorithms Articles You may like

Thanks for reading this article so far. If you like this Java linked list tutorials and my explanation then please share it with your friends and colleagues. If you have any questions or doubt then please write a comment and I'll try to find an answer for you.

P. S. - If you are new to the Java Programming world and want to learn Data Structure and Algorithms in Java and looking for some free courses to start with then you should also check out my list of FREE Data Structure and Algorithm courses for Java Developers which contains free data structure and algorithms courses from Udemy, Coursera, and other popular online websites. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.