Introduction
In this article, we describe the use of the LinkList class in Java. I am sure most of you have implemented a linked list in the C language because it is a very popular data structure. Java directly provides a LinkList class within java. util package. So you will be creating an object of a link list class and performing various operations like adding and removing objects. This class extends AbstractSequentialList and implements List, Cloneable, Serializable. It permits all elements including null. A LinkedList class provides methods to get, insert and remove an element at the beginning and end of the list.
Note- The LinkedList class is a doubly-linked list, which internally maintains references to the previous and next element at each node in the list.
Use of getFirst() and getLast() method of LinkList class
In the linked list you can directly access the first and last element of the linklist by using its methods getfirst() and getLast(). And both methods throws NoSuchElementException Exceptions - if this list is empty.
Syntax
public Object getFirst()
public Object getLast()
public Object getLast()
Example
- import java.util.LinkedList;
- public class GetFirstAndtLastDemo
- {
- public static void main(String[] args)
- {
- LinkedList<String> lList = new LinkedList<String>();
- lList.add("abhishek dubey");
- lList.add("amit gupta");
- lList.add("anrudh pandey");
- lList.add("sourabh tripathi");
- lList.add("omji dubey");
- System.out.println("First element of LinkedList is : "
- + lList.getFirst());
- System.out.println("Last element of LinkedList is : " + lList.getLast());
- }
- }
Output
Getting the sublist from the LinkList
Syntax
public List subList(int start, int end);
Example


Comments
Join the conversation! Your thoughts help the community grow.