CodesJava

Easy learning with example program codes

LinkedList in java


LinkedList:

The LinkedList class extends AbstractSequentialList and implements the List and Deque interface. It uses linked list data structure to store elements. It can contain duplicate elements. It is not synchronized.

Note: It not provides the random access facility.

 LinkedList example:

LinkedListTest.java

import java.util.Iterator;
import java.util.LinkedList;
 
/**
 * This class is used to show the LinkedList functionality.
 * @author codesjava
 */
public class LinkedListTest {
	public static void main(String args[]){
		//Create LinkedList object.
		LinkedList linkedList = new LinkedList();
 
		//Add objects to the HashSet.
		linkedList.add("Amani");
		linkedList.add("Prabhjot");
		linkedList.add("Nidhi");
		linkedList.add("Vandana");
		linkedList.add("Poonam");
 
		//Size of the LinkedList object.
		System.out.println("Size: " 
				+ linkedList.size());
 
		//Print the LinkedList object.
		System.out.println("LinkedList elements:");
		System.out.println(linkedList);
 
		//Print the LinkedList elements using iterator.
		Iterator iterator1=linkedList.iterator();
		System.out.println("LinkedList elements " +
				"using iterator:");
		while(iterator1.hasNext()){  
		   System.out.println(iterator1.next());  
		} 
 
		//Add an object at the specific position.
		linkedList.add(2,"Jagdeep");
 
		//Remove a element from a specific position.
		linkedList.remove(3);
 
		//Remove last element.
		linkedList.removeLast();
 
		//Size of the LinkedList object.
		System.out.println("Size after manipulation: " 
				+ linkedList.size());
 
		//Print the LinkedList object.
		System.out.println("LinkedList elements " +
				"after manipulation:");
		System.out.println(linkedList);
 
		//Print the LinkedList elements using iterator.
		Iterator iterator2=linkedList.iterator();
		System.out.println("LinkedList elements after " +
				"manipulation using iterator:");
		while(iterator2.hasNext()){  
		   System.out.println(iterator2.next());  
		} 	
	}
}

Output:

Size: 5
LinkedList elements:
[Amani, Prabhjot, Nidhi, Vandana, Poonam]
LinkedList elements using iterator:
Amani
Prabhjot
Nidhi
Vandana
Poonam
Size after manipulation: 4
LinkedList elements after manipulation:
[Amani, Prabhjot, Jagdeep, Vandana]
LinkedList elements after manipulation using iterator:
Amani
Prabhjot
Jagdeep
Vandana

Download this example.
 
Next Topic: HashMap in java with example.
Previous Topic: ArrayList in java with example.



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki