HudaTutorials.com

java util LinkedList Class - LinkedList Class in Java

Last updated on

java.util.LinkedList Class

LinkedList Class in Java

The LinkedList class is a doubly linked list, which internally maintains references to the previous and next element at each node in the list. Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). The class implements the Deque interface, providing first-in-first-out queue operations for add, poll, along with other stack and deque operations.

Is LinkedList Class Thread Safe ?

The LinkedList implementation is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

Note : To prevent accidental unsynchronized access to the list, you should create list with Collections.synchronizedList method.

In this tutorial you can learn about java.util.LinkedList class and its examples. And also learn how to use java.util.LinkedList class.

java.util.LinkedList class Example

/*	Java LinkedList class Example
	Save with file name LinkedListExample.java	*/
import java.util.LinkedList;
public class LinkedListExample
{
	public static void main(String args[])
	{
		// java.util.LinkedList DECLARATION
		LinkedList al;
		// java.util.LinkedList OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		al = new LinkedList();
		// ADD AN ELEMENT
		al.add("Huda Tutorials");
		al.add("Java Tutorials");
		// DUPLICATES ARE ALLOWED
		al.add("Huda Tutorials");
		al.add("C Tutorials");
		al.add("CPP Tutorials");
		// null ALLOWED
		al.add(null);
		// RETURNS COUNT OF ELEMENTS LinkedList CONTAINS
		System.out.println("Elements Count : " + al.size());
		// java.util.LinkedList OUTPUT
		System.out.println(al);
	}
}

How to use LinkedList with Iterator

The following example shows how to use LinkedList with Iterator.

/*	How to use LinkedList with Iterator Example
	Save with file name LinkedListExample2.java	*/
import java.util.LinkedList;
import java.util.Iterator;
public class LinkedListExample2
{
	public static void main(String args[])
	{
		// java.util.LinkedList DECLARATION
		LinkedList al;
		// java.util.LinkedList OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		al = new LinkedList();
		// ADD AN ELEMENT
		al.add("Huda Tutorials");
		al.add("Java Tutorials");
		// DUPLICATES ARE ALLOWED
		al.add("Huda Tutorials");
		al.add("C Tutorials");
		al.add("CPP Tutorials");
		// null ALLOWED
		al.add(null);
		// RETURNS COUNT OF ELEMENTS LinkedList CONTAINS
		System.out.println("Elements Count : " + al.size());
		// java.util.LinkedList OUTPUT
		Iterator itr = al.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}

How to use LinkedList with Arrays, List and Iterator

The following example shows how to use LinkedList with Arrays, List and Iterator.

/*	How to use LinkedList with Arrays, List and Iterator Example
	Save with file name LinkedListExample3.java	*/
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;
public class LinkedListExample3
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		List s; // java.util.List DECLARATION
		// java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new LinkedList(Arrays.asList(elements));
		// java.util.List OUTPUT
		Iterator itr = s.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}

java.util.LinkedList class Example 4

/*	Java LinkedList class Example 4
	Save with file name LinkedListExample4.java	*/
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;
public class LinkedListExample4
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		List s; // java.util.List DECLARATION
		// java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new LinkedList(Arrays.asList(elements));
		// ADD ELEMENT TO List COLLECTION
		s.add("NetBeans Tutorials");
		// DISPLAY SIZE OF THE List COLLECTION
		System.out.println("List Collection Size : "+s.size());
		// CHECK THE List COLLECTION IS EMPTY
		System.out.println("List Collection is Empty : "+s.isEmpty());
		// CHECK THE GIVEN ELEMENT IN List COLLECTION
		System.out.println("\"Huda Tutorials\" Contains :"+s.contains("Huda Tutorials"));
		// java.util.List OUTPUT
		System.out.println("Elements Before Element Remove");
		Iterator itr = s.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
		s.remove("C Tutorials");
		System.out.println("Elements After Element Remove");
		itr = s.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}

How to compare two List Collections

The following example shows how to compare two List Collections.

/*	How to compare two List Collections Example
	Save with file name LinkedListExample5.java	*/
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;
public class LinkedListExample5
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		String elements2[] = {"Huda Tutorials","Java Tutorials","Android Tutorials"};
		// java.util.List DECLARATION
		List s, s2;
		// java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new LinkedList(Arrays.asList(elements));
		s2 = new LinkedList(Arrays.asList(elements2));
		// COMPARE TWO COLLECTIONS
		System.out.println("Equals : " + s2.equals(s));
		// CONTAINS COLLECTION IN OTHER COLLECTION
		System.out.println("Contains : " + s2.containsAll(s));
	}
}

How to save List Collection into file

The following example shows how to save List Collection into file.

/*	How to save List Collection into file Example
	Save with file name LinkedListExample6.java	*/
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class LinkedListExample6
{
	public static void main(String args[])
	{
		try
		{
			String elements[] = {"Huda Tutorials","Java Tutorials","Android Tutorials"};
			// java.util.LinkedList DECLARATION
			List s;
			// java.util.List OBJECT CREATION USING ARRAY AS LIST
			s = new LinkedList(Arrays.asList(elements));
			// FileOutputStream CREATION
			FileOutputStream fos = new FileOutputStream("List.set");
			// ObjectOutputStream CREATION
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			// WRITE List OBJECT TO ObjectOutputStream
			oos.writeObject(s);
			// CLOSE THE ObjectOutputStream
			oos.close();
			System.out.println("List Collection Saved into File Sucessfully");
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e.getMessage());
		}
	}
}

How to retrieve List Collection from file

The following example shows how to retrieve List Collection from file.

/*	How to retrieve List Collection from file Example
	Save with file name LinkedListExample7.java	*/
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class LinkedListExample7
{
	public static void main(String args[])
	{
		try
		{
			// java.util.LinkedList DECLARATION
			List s;
			// FileInputStream CREATION
			FileInputStream fis = new FileInputStream("List.set");
			// ObjectInputStream CREATION
			ObjectInputStream ois = new ObjectInputStream(fis);
			// READ List OBJECT FROM ObjectInputStream
			s = (List) ois.readObject();
			ois.close();
			System.out.println(s);
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e.getMessage());
		}
	}
}

How to use LinkedList with Arrays, Iterator and ListIterator

The following example shows how to use LinkedList with Arrays, Iterator and ListIterator.

/*	How to use LinkedList with Arrays, Iterator and ListIterator Example
	Save with file name LinkedListExample8.java	*/
import java.util.LinkedList;
import java.util.Arrays;
import java.util.ListIterator;
import java.util.Iterator;
public class LinkedListExample8
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		LinkedList s; // java.util.LinkedList DECLARATION
		// java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new LinkedList(Arrays.asList(elements));
		// java.util.List OUTPUT
		ListIterator itr = s.listIterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
		System.out.println("----- Reverse Order -----");
		// java.util.List OUTPUT IN REVERSE ORDER
		Iterator itr2 = s.descendingIterator();
		while(itr2.hasNext())
		{
			System.out.println(itr2.next());
		}
	}
}

java.util.LinkedList class Example 9

/*	Java LinkedList class Example 9
	Save with file name LinkedListExample9.java	*/
import java.util.LinkedList;
public class LinkedListExample9
{
	public static void main(String args[])
	{
		// java.util.LinkedList DECLARATION
		LinkedList al;
		// java.util.LinkedList OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		al = new LinkedList();
		// ADD AN ELEMENT
		al.addFirst("Java Tutorials");
		// ADD AN ELEMENT AT LAST
		al.addLast("C Tutorials");
		al.addLast("CPP Tutorials");
		// ADD AN ELEMENT AT FIRST
		al.addFirst("Huda Tutorials2");
		// ADD AN ELEMENT
		al.push("Huda Tutorials");
		// java.util.LinkedList OUTPUT
		System.out.println(al);
		// GET THE FIRST ELEMENT
		System.out.println("First Element : " + al.getFirst());
		// GET THE LAST ELEMENT
		System.out.println("Last Element : " + al.getLast());
		// GET THE HEAD (FIRST) ELEMENT
		System.out.println("Head (First) Element : " + al.peek());
	}
}