Java – LinkedList specific method examples

In this article, we will discuss simple example on LinkedList specific methods i.e.; what are the various methods available in LinkedList class and how it can be operated on LinkedList objects

1. LinkedList specific method and its description:

LinkedList methodsDescription
void addFirst(Object obj);add/inserts the specified element/object at the beginning of the invoking list
void addLast(Object obj);add/inserts the specified element/object at the end of the invoking list
Object getFirst();returns first element/object from invoking list

 

throws NoSuchElementException; if list is empty

Object getLast();returns last element/object from invoking list

 

throws NoSuchElementException; if list is empty

Object removeFirst();removes & returns first element/object from invoking list

 

throws NoSuchElementException; if list is empty

Object removeLast();removes & returns last element/object from invoking list

 

throws NoSuchElementException; if list is empty

2. Example on LinkedList operations with all its methods :

LinkedListSpecificMethods.java

package in.bench.resources.java.collections;

import java.util.LinkedList;

public class LinkedListSpecificMethods {

	public static void main(String[] args) {

		// creating LinkedList object of type String
		LinkedList<String> ll = new LinkedList<String>();

		// adding elements to LinkedList object
		ll.add("Sun");
		ll.add("Apple");
		ll.add("JBoss");
		ll.add("Whatsup");
		ll.add("Android");
		ll.add("BEA Weblogic");
		ll.add("Apache");

		// Iterating using enhanced for-loop
		System.out.println("LinkedList contents "
				+ "as per Insertion Order:\n");
		for(String str : ll) {
			System.out.println(str);
		}

		// LinkedList specific method examples

		// getting 1st and last elements of LinkedList
		String strFirst = ll.getFirst();
		String strLast = ll.getLast();

		System.out.println("\n\nFirst element of LinkedList : "
				+ strFirst);
		System.out.println("Last element of LinkedList : "
				+ strLast);

		// adding First and Last elements to LinkedList
		ll.addFirst("Instagram");
		ll.addLast("Pinterest");

		// Iterating using enhanced for-loop
		System.out.println("\n\nIterating LinkedList "
				+ "after adding First & Last elements:\n");
		for(String str : ll) {
			System.out.println(str);
		}

		// removing First and Last elements of LinkedList
		String strRemoveFirst = ll.removeFirst();
		String strRemoveLast = ll.removeLast();

		System.out.println("\n\nFirst element removed is : "
				+ strRemoveFirst);
		System.out.println("Last element removed is : "
				+ strRemoveLast);

		// Iterating using enhanced for-loop
		System.out.println("\n\nIterating LinkedList "
				+ "after removing First & Last elements:\n");
		for(String str : ll) {
			System.out.println(str);
		}
	}
}

Output:

LinkedList contents as per Insertion Order:

Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache

First element of LinkedList : Sun
Last element of LinkedList : Apache

Iterating LinkedList after adding First & Last elements:

Instagram
Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache
Pinterest

First element removed is : Instagram
Last element removed is : Pinterest

Iterating LinkedList after removing First & Last elements:

Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 5 - Queue’s push and pop operations with LinkedList
Java - How to iterate through LinkedList in reverse-order ?