Working With LinkList Class in Java


Introduction

In this article we describe 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 with in 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

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

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 second end point.

Syntax
public List subList(int start, int end);

Example

import java.util.LinkedList;
import java.util.List;
public class SubListDemo
{
public static void main(String[] args)
{
LinkedList<String> lList =
new LinkedList<String>();
lList.add(
"Abhishek");
lList.add(
"Amit");
lList.add(
"Omji");
List.add(
"Anoop");
lList.add(
"Sourabh");
lList.add(
"sanjoli");
lList.add(
"sachin");
lList.add(
"priyanka");
System.out.println(lList);
System.out.println();
List subl1 = lList.subList(2, 6);
System.out.print(
"SubList : ");
System.out.println(subl1);
System.out.println();
subl1.remove(2);
System.out.print(
"Remove2 element from Sublist:");
System.out.println(subl1);
System.out.println();
System.out.println(lList);
}
}

Output

SubListDemocmd.jpg 

Adding Element First and Last positions

The linkList class provides addFirst() and addLast() to 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

import java.util.LinkedList;
public class AddFirstAndLast
{
public static void main(String[] args)
{
LinkedList<String> lList =
new LinkedList<String>();
lList.add(
"Abhishek");
lList.add(
"Amit");
lList.add(
"Omji");
lList.add(
"Sourabh");
System.out.println(
"Your list is :");
System.out.println(lList);
lList.addFirst(
"Rajesh");
System.out.println(
"After Adding rajesh at first :");
System.out.println(lList);
lList.addLast(
"Sumit");
System.out.println(
"After Sumit Add at last :");
System.out.println(lList);
}
}

Output

AddFirstAndLastcmd.jpg

Resources

How to use FileWriter and FileReaderClass in JAVA
Abstract Class in JAVA
Standalone JAVA Class using Apache HTTP Client
Working with the BigDecimal class in JAVA
How to Find All the Constructors, Fields and Methods of a Class in JAVA

Up Next
    Ebook Download
    View all
    Learn
    View all