Working With LinkList Class in Java

In this article we describe use of the LinkList class in Java.

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()
 
Example
  1. import java.util.LinkedList;  
  2. public class GetFirstAndtLastDemo  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         LinkedList<String> lList = new LinkedList<String>();      
  7.         lList.add("abhishek dubey");     
  8.         lList.add("amit gupta");    
  9.         lList.add("anrudh pandey");   
  10.         lList.add("sourabh tripathi");   
  11.         lList.add("omji dubey");  
  12.  
  13.         System.out.println("First element of LinkedList is : "   
  14.         + lList.getFirst());    
  15.         System.out.println("Last element of LinkedList is : " + lList.getLast());
  16.     }  
  17.  
Output
 
GetFirstAndtLastDemocmd.jpg
 

Getting the sublist from the LinkList

 
In the linked list you can make a sub-list from a linklist by using a method subList(). It is a method of the AbstractList class and the AbstractList class is a parent class of the LinkList class. It takes two arguments as an integer; first start point and the second endpoint.
 
Syntax
 
public List subList(int start, int end);
 
Example 
  1. import java.util.LinkedList;  
  2. import java.util.List;  
  3. public class SubListDemo  
  4. {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         LinkedList<String> lList = new LinkedList<String>();  
  8.         lList.add("Abhishek");  
  9.         lList.add("Amit");  
  10.         lList.add("Omji");  
  11.         List.add("Anoop");  
  12.         lList.add("Sourabh");  
  13.         lList.add("sanjoli");  
  14.         lList.add("sachin");  
  15.         lList.add("priyanka");  
  16.         System.out.println(lList);  
  17.         System.out.println();  
  18.           
  19.         List subl1 = lList.subList(26);  
  20.         System.out.print("SubList : ");  
  21.         System.out.println(subl1);  
  22.         System.out.println();  
  23.         subl1.remove(2);  
  24.         System.out.print("Remove2 element from Sublist:");  
  25.         System.out.println(subl1);  
  26.         System.out.println();  
  27.         System.out.println(lList);  
  28.     }  
  29.  
Output
 
SubListDemocmd.jpg 
 

Adding Element First and Last positions

 
The LinkedList class provides addFirst() and addLast() to a method to add the element at directly on first and last position by using these methods. 
 
Syntax
 
public void addFirst(Object o)
public void addLast(Object o)
 
Example
  1. import java.util.LinkedList;  
  2. public class AddFirstAndLast  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         LinkedList<String> lList = new LinkedList<String>();  
  7.         lList.add("Abhishek");  
  8.         lList.add("Amit");  
  9.         lList.add("Omji");  
  10.         lList.add("Sourabh");  
  11.         System.out.println("Your list is :");  
  12.         System.out.println(lList);  
  13.         lList.addFirst("Rajesh");  
  14.         System.out.println("After Adding rajesh at first :");  
  15.         System.out.println(lList);  
  16.         lList.addLast("Sumit");  
  17.         System.out.println("After Sumit Add at last :");  
  18.         System.out.println(lList);  
  19.     }  
  20.  
Output
 
AddFirstAndLastcmd.jpg
 
Resources