- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Retrieve the last element from a LinkedList in Java
The last element of a Linked List can be retrieved using the method java.util.LinkedList.getLast() respectively. This method does not require any parameters and it returns the last element of the LinkedList.
A program that demonstrates this is given as follows −
Example
import java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList<String> l = new LinkedList<String>(); l.add("Andy"); l.add("Sara"); l.add("James"); l.add("Betty"); l.add("Bruce"); System.out.println("The last element of the Linked List is : " + l.getLast()); } }
Output
The last element of the Linked List is : Bruce
Now let us understand the above program.
The LinkedList l is created. LinkedList.add() is used to add the elements to the LinkedList. Then LinkedList.getLast() is used to retrieve the last element of the linked list. A code snippet which demonstrates this is as follows −
LinkedList<String> l = new LinkedList<String>(); l.add("Andy"); l.add("Sara"); l.add("James"); l.add("Betty"); l.add("Bruce"); System.out.println("The last element of the Linked List is : " + l.getLast());
- Related Articles
- Delete first and last element from a LinkedList in Java
- Remove a specific element from a LinkedList in Java
- Get first and last elements from Java LinkedList
- Retrieve an element from ArrayList in Java
- Get the last element from a Sorted Set in Java
- Retrieve the last entry in the TreeMap in Java
- Search a particular element in a LinkedList in Java
- Replace an element of a Java LinkedList
- Add a single element to a LinkedList in Java
- How to remove an element from ArrayList or, LinkedList in Java?
- How do you find the element of a LinkedList in Java?
- Implement a stack from a LinkedList in Java
- Retrieve element from local storage in JavaScript?
- Get SubList from LinkedList in Java
- How to retrieve the contents of a ResultSet from last to first in JDBC?

Advertisements