Java LinkedList Class

Java LinkedList Class | What is LinkedList Class in Java Collecton?

Hello Readers in this article we are going to learn about JAVA
Collection LinkedList Class.

LinkedList Class in JAVA Collection is a Class which can be used to hold heterogeneous types of elements as Array allowed only homogeneous type of elements and it is fixed in size.

To overcome these problem we should go for Java Collection who provide several classes.Here we are looking for LinkedList Class.

Declaration/Hierarchy of Java Collection LinkedList  Class.

public class LinkedList<E>  extends AbstractSequentialList<E>

implements List<E>, Deque<E>, Cloneable,

LinkedList Class
The underlying data structure is doubly linked list.
It uses doubly linked list to store the elements.
In doubly Linked List case we add or remove elements from both side.
Insertion order is preserved.
It allows duplicate elements to be stored.
It allows null also.
     It is not implements Random Access.
     It better for to perform manipulation in between.

     It is not recommended to perform retrieval operations.

Capacity concept not applicable in the case of LinkedList.

Java LinkedList Class is not Synchronized.

LinkedList extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable,

Constructors of LinkedList Class.


It creates an empty LinkedList.

2.LinkedList(Collection c);

It is used to creates an equivalent LinkedList object for the
given or specified Collection.


boolean add(Object o)  

It is used to add the specified element to the end of a list.

void add(int index, Object element)  

It is used to add the elements at the specified index in list.

void addFirst(Object o) 

It is used to add element at the starting of a list.

void addLast(Object o)  

It is used to add the element to the end of a list.

Object getFirst()              

It is used to return the first element from a list.

Object getLast()              

It is used to return the last element from a list.

boolean contains(Object o)    

It is used to return true if the list contains the given element.

boolean remove(Object o) 

It is used to remove the first occurrence of the specified element from a list.

int indexOf(Object o)     

It is used to returns the index from a list of the first occurrence of the given element, it will return -1 if the list does not contain given element.

int lastIndexOf(Object o)             

It is used to return the index in a list of the last occurrence of the given element, or it will return -1 if the list does not contain given element.

int size()               

It is used to return the size/number of elements present in a list.

Java LinkedList Examples

In this example we will learn.

How to add elements in LinkedList?

How to print the elements of LinkedList?

How to get first element?

How to get last element?

How to get size of LinkedList?

How to get first occurrence of elements?

How to get last occurrence of elements?

How to check this list contains specific element?

How to remove elements?

How to remove first element from LinkedList?

How to remove last element LinkedList?

How to add elements of LinkedList to another LinkedList using addAll(Collection c) method?

How to add element at first index in LinkedList using addFirst() method?

How to add element at last index in LinkedList using addLast() method?

How to get elements of LinkedList one by one?

import java.util.*;

public class TestLinkedList{

public static void main(String[] args){

LinkedList<String> ll=new LinkedList<String>();







System.out.println("Normal Linked List");



System.out.println("Getting 1st Eement");



System.out.println("Getting last Eement");



System.out.println("Size of list "+ll.size());

System.out.println("First Occurrence of Matin "+ll.indexOf("Matin"));

System.out.println("Lasst Occurrence of Matin "+ll.lastIndexOf("Matin"));

System.out.println("Is this list contains Kishan "+ll.contains("Kishan"));

System.out.println("Is list contains Kunal "+ll.contains("kunal"));


ll.remove("Manish");//removing manish



ll.removeFirst();//removing first element


ll.removeLast();//removing last element

System.out.println(“After removeFirst() and removeLast() ”+ll);


LinkedList<String> ls=new LinkedList<>();



ll.addAll(ls);//adding ls to ll at the end of ll

ll.addFirst("First Element");//adding to first in ll

ll.addLast("Last Element");");//adding to lasst in ll

System.out.println("After Addition of ls now Normal Linked List "+ll);

//Getting List elements one by one

System.out.println("Getting List Elements one by one");

for(String s:ll){



      }//end of main

}//end of class


Normal Linked List

[Matin, Kishan, Dilshad, Manish, Chandan, Matin]

Getting 1st Eement


Getting last Eement


Size of list 6

First Occurrence of Matin 0

Lasst Occurrence of Matin 5

Is this list contains Kishan true

Is list contains Kunal false

[Matin, Kishan, Dilshad, Chandan, Matin]

[Kishan, Dilshad, Chandan, Matin]

After removeFirst() and removeLast()[Kishan, Dilshad, Chandan]

After Addition of ls now Normal Linked List [First Element, Kishan, Dilshad, Chandan, Cricket, FootBall, Last Element]

Getting List Elements one by one

First Element






Last Element

Java SportsLinkedList Examples
In this example we will see how to add user defined class object into LinkedList, first we have created a class named as SportLinkedList with id,sportname and city name and a parameterized constructor.

We have overridden a toString() method fot textual representation of object.After that we have created the object of SportLinkedList class by passing the required arguments and added then to LinkedList using add() method and displaying them.

import java.util.*;

public class SportLinkedList {

int id;

String sportname,cityname;

public SportLinkedList(int id, String sportname, String cityname) { = id;





public String toString() {

return id + "," + sportname + "," + cityname;


//Main Method

public static void main(String[] args) {

SportLinkedList sll1=new SportLinkedList(1, "Cricket", "Mumbai"); 

 SportLinkedList sll2=new SportLinkedList(2, "FootBall", "Assam"); 

 SportLinkedList sll3=new SportLinkedList(3, "Hockey", "Punjab"); 

LinkedList<SportLinkedList> ll=new LinkedList<SportLinkedList>(); 


System.out.println("id Sport City");  
for(SportLinkedList ss:ll) {  System.out.println(ss);                      }

  }//End of main

}//End of class 

id   Sport   City 1,Cricket,Mumbai2,FootBall,Assam 3,Hockey,Punjab

Hope it was helpful
If you want to become a leader then you should be a good reader.

See also

No comments

You May Like Also

Programming Knowledge

Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database