Add, remove, retain, sort methods of linkedlist collection (java / example)

What is LinkedList collection?

  • The LinkedList class extends AbstractSequentialList and implements the List interface.
  • Important methods of LinkedList collection?
    • Remove
    • Add
    • Retain
    • Sort
    • Iterate
    • Get
    • Contains And
    • Clear

Program – Add, remove, retain, sort methods of linkedlist

package org.learn.collection.list.linkedlist;

import java.util.Collections;
import java.util.LinkedList;
import java.util.ListIterator;

public class DemoLinkedList {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<>();
		linkedList.add("archery");
		linkedList.add("badminton");
		linkedList.add("canoe");
		linkedList.add("boxing");
		linkedList.add("canoe");
		linkedList.add("diving");
		linkedList.add("badminton");
		linkedList.add("diving");

		System.out.println("Demo of remove methods: ");
		demoRemoveMethod(linkedList);

		System.out.println("\nDemo of add methods: ");
		demoAddMethod(linkedList);

		System.out.println("\nDemo of retain method: ");
		demoRetainAll(linkedList);

		System.out.println("\nSort elements of linkedList: ");
		demoSortMethod(linkedList);

		System.out.println("\nIterate through linkedList: ");
		demoIterateMethod(linkedList);

		//[golf, diving, archery]
		System.out.println("\nDemo of get method of linkedlist: ");
		//golf
		System.out.println("Element at index 0: " + linkedList.get(0));
		//archery
		System.out.println("Element at index 2: " + linkedList.get(2));

		//[golf, diving, archery]
		System.out.println("\nDemo of contains method of linkedlist: ");
		System.out.println("Check linkedlist contains diving: " + linkedList.contains("diving"));
		System.out.println("Check linkedlist contains judo: " + linkedList.contains("judo"));

		System.out.println("\nDemo of clear method of linkedlist: ");
		linkedList.clear();
		System.out.println("Clear linkedList: " + linkedList);
	}

	private static void demoRemoveMethod(LinkedList<String> linkedList) {
		// [archery, badminton, canoe, boxing, diving, badminton, diving]
		System.out.println("Orignal LinkedList:" + linkedList);

		// Remove element by index
		String removedElement = linkedList.remove(2);
		// [archery, badminton, boxing, diving, badminton, diving]
		System.out.printf("1. Removed element %s from index 2: %s \n", removedElement, linkedList);

		// Remove element by index
		removedElement = linkedList.removeFirst();
		// [badminton, boxing, diving, badminton, diving]
		System.out.printf("2. Removed first element %s of linked list:%s \n", removedElement, linkedList);

		// Remove element by index
		removedElement = linkedList.removeLast();
		// [badminton, boxing, diving, badminton]
		System.out.printf("3. Removed last element %s of linked list:%s \n", removedElement, linkedList);

		// Remove element after comparing and remove it
		linkedList.remove("boxing");
		// [badminton, diving, badminton]
		System.out.println("4. Removed boxing from linkedList: " + linkedList);

		LinkedList<String> removeElementsList = new LinkedList<>();
		removeElementsList.add("diving");

		// Remove element by supplying another collection
		linkedList.removeAll(removeElementsList);

		// [badminton, badminton]
		System.out.println("5. Removed collection containing diving: " + linkedList);

		// Remove element by supplying another collection
		linkedList.removeLastOccurrence("badminton");

		// [badminton, badminton]
		System.out.println("6. Removed last occurence of badminton: " + linkedList);

		// Remove element by supplying another collection
		linkedList.removeFirstOccurrence("badminton");

		// [badminton, badminton]
		System.out.println("7. Removed first occurence of badminton: " + linkedList);
	}

	private static void demoAddMethod(LinkedList<String> linkedList) {
		// [canoe]
		System.out.println("1. Orignal LinkedList:" + linkedList);
		linkedList.add(0, "archery");
		linkedList.add(2, "canoe");
		// [archery, canoe, canoe]
		System.out.println("2. Added element at 0 and 2 index: " + linkedList);

		linkedList.add("diving");
		// [archery, canoe, canoe, diving]
		System.out.println("3. Added element in list: " + linkedList);

		LinkedList<String> addElementsList = new LinkedList<>();
		addElementsList.add("squash");
		addElementsList.add("bowling");

		// It will add elements to last of list
		linkedList.addAll(addElementsList);
		// [archery, canoe, canoe, diving, squash, bowling]
		System.out.println("4. Added another list : " + linkedList);

		LinkedList<String> anotherList = new LinkedList<>();
		anotherList.add("golf");
		anotherList.add("judo");

		// It will add elements from index 1
		linkedList.addAll(1, anotherList);
		// [archery, golf, judo, canoe, canoe, diving, squash, bowling]
		System.out.println("5. Added another list at index 1 : " + linkedList);

		// It will add element before head i.e. first element of list of list
		linkedList.addFirst("squash");
		//  [squash, archery, golf, judo, canoe, canoe, diving, squash, bowling]
		System.out.println("6. Added element before head : " + linkedList);

		// It will add elements to last of list
		linkedList.addLast("judo");
		// [squash, archery, golf, judo, canoe, canoe, diving, squash, bowling, judo]
		System.out.println("7. Added element to after tail node : " + linkedList);
	}

	private static void demoRetainAll(LinkedList<String> linkedList) {
		// [squash, archery, golf, judo, canoe, canoe, diving, squash, bowling, judo]
		System.out.println("Orignal LinkedList:" + linkedList);
		LinkedList<String> retainSportsList = new LinkedList<>();
		retainSportsList.add("golf");
		retainSportsList.add("diving");
		retainSportsList.add("archery");
		retainSportsList.add("badminton");
		System.out.println("Elements to be retained:"+retainSportsList);
		linkedList.retainAll(retainSportsList);
		//[archery, golf, diving]
		System.out.println("Elements retained in orignal linkedlist: " + linkedList);
	}

	private static void demoSortMethod(LinkedList<String> linkedList) {
		//[archery, golf, diving]
		System.out.println("Orignal LinkedList:" + linkedList);
		Collections.sort(linkedList);
		// [archery, diving, golf]
		System.out.println("1. Sort ascending order: " + linkedList);

		Collections.sort(linkedList, Collections.reverseOrder());
		// [golf, diving, archery]
		System.out.println("2. Sort desending order: " + linkedList);
	}

	private static void demoIterateMethod(LinkedList<String> linkedList) {

		System.out.println("1. Iterating using forEachRemaining:");
		// Output of for loop:
		// golf diving archery 
		linkedList.forEach(element -> System.out.printf("%s ", element));

		System.out.println("\n2. Iterating linkedList using foreach loop:");
		// Output of for loop:
		// golf diving archery 
		for (String element : linkedList) {
			System.out.printf("%s ", element);
		}

		System.out.println("\n3. Iterating linkedList using iterator:");
		ListIterator<String> listIterator = linkedList.listIterator();
		// Output of while loop:
		// golf diving archery 
		while (listIterator.hasNext()) {
			System.out.printf("%s ", listIterator.next());
		}
		System.out.println("");
	}

}

OutputĀ – Add, remove, retain, sort etc. methods of linkedlist

Demo of remove methods: 
Orignal LinkedList:[archery, badminton, canoe, boxing, canoe, diving, badminton, diving]
1. Removed element canoe from index 2: [archery, badminton, boxing, canoe, diving, badminton, diving] 
2. Removed first element archery of linked list:[badminton, boxing, canoe, diving, badminton, diving] 
3. Removed last element diving of linked list:[badminton, boxing, canoe, diving, badminton] 
4. Removed boxing from linkedList: [badminton, canoe, diving, badminton]
5. Removed collection containing diving: [badminton, canoe, badminton]
6. Removed last occurence of badminton: [badminton, canoe]
7. Removed first occurence of badminton: [canoe]

Demo of add methods: 
1. Orignal LinkedList:[canoe]
2. Added element at 0 and 2 index: [archery, canoe, canoe]
3. Added element in list: [archery, canoe, canoe, diving]
4. Added another list : [archery, canoe, canoe, diving, squash, bowling]
5. Added another list at index 1 : [archery, golf, judo, canoe, canoe, diving, squash, bowling]
6. Added element before head : [squash, archery, golf, judo, canoe, canoe, diving, squash, bowling]
7. Added element to after tail node : [squash, archery, golf, judo, canoe, canoe, diving, squash, bowling, judo]

Demo of retain method: 
Orignal LinkedList:[squash, archery, golf, judo, canoe, canoe, diving, squash, bowling, judo]
Elements to be retained:[golf, diving, archery, badminton]
Elements retained in orignal linkedlist: [archery, golf, diving]

Sort elements of linkedList: 
Orignal LinkedList:[archery, golf, diving]
1. Sort ascending order: [archery, diving, golf]
2. Sort desending order: [golf, diving, archery]

Iterate through linkedList: 
1. Iterating using forEachRemaining:
golf diving archery 
2. Iterating linkedList using foreach loop:
golf diving archery 
3. Iterating linkedList using iterator:
golf diving archery 

Demo of get method of linkedlist: 
Element at index 0: golf
Element at index 2: archery

Demo of contains method of linkedlist: 
Check linkedlist contains diving: true
Check linkedlist contains judo: false

Demo of clear method of linkedlist: 
Clear linkedList: []
Scroll to Top