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.

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, java.io.Serializable
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, java.io.Serializable.
Constructors of LinkedList Class.
1.LinkedList();
It creates an empty LinkedList.
2.LinkedList(Collection c);
It is used to creates an equivalent LinkedList object for the
given or specified Collection.
Method's
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>();
ll.add("Matin");
ll.add("Kishan");
ll.add("Dilshad");
ll.add("Manish");
ll.add("Chandan");
ll.add("Matin");
System.out.println("Normal
Linked List");
System.out.println(ll);
System.out.println("Getting
1st Eement");
System.out.println(ll.getFirst());
System.out.println("Getting
last Eement");
System.out.println(ll.getLast());
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
System.out.println(ll);
ll.removeFirst();//removing
first element
System.out.println(ll);
ll.removeLast();//removing
last element
System.out.println(“After
removeFirst() and removeLast() ”+ll);
LinkedList<String> ls=new LinkedList<>();
ls.add("Cricket");
ls.add("FootBall");
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){
System.out.println(s);
}
}//end of main
}//end
of class
output:
Normal Linked List
[Matin, Kishan, Dilshad,
Manish, Chandan, Matin]
Getting 1st Eement
Matin
Getting last Eement
Matin
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
Kishan
Dilshad
Chandan
Cricket
FootBall
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) {
this.id = id;
this.sportname= sportname;
this.cityname= cityname;
}
@Override
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>();
ll.add(sll1);
ll.add(sll2);
ll.add(sll3);
System.out.println("id Sport City");
for(SportLinkedList ss:ll) { System.out.println(ss);
}
}//End of main
}//End of class
Output:
id Sport City 1,Cricket,Mumbai2,FootBall,Assam 3,Hockey,Punjab
No comments