Java Collections Framework:LinkedList-2(Add/Remove)

 

Adding an element to LinkedList using add(E e) method – Java


Description

Program to add a new element to LinkedList using add(E e) method of LinkedList class.

Example

import java.util.LinkedList;
class LinkedListAdd {

  public static void main(String[] args) {

     // create a LinkedList
     LinkedList<String> list = new LinkedList<String>();

     // Adding elements to the LinkedList
     list.add("Harry");
     list.add("Ajeet");
     list.add("Tom");
     list.add("Steve");

     // Displaying LinkedList elements
     System.out.println("LinkedList elements: "+list);
 
     // Adding another element
     list.add("Kate");
 
     // Displaying LinkedList elements after add(E e)
     System.out.println("LinkedList elements: "+list);
  }
}

Output:

LinkedList elements: [Harry, Ajeet, Tom, Steve]
LinkedList elements: [Harry, Ajeet, Tom, Steve, Kate]

LinkedList.add(E e) method

public boolean add(E e): Appends the specified element to the end of this list.
This method is equivalent to addLast(E). More at: Java LinkedList.add(E e)

Java – Add element at specific index in LinkedList example


In this tutorial we will learn how to add a new element at specific index in LinkedList. We will be using add(int index, Element E) method of LinkedList class to perform this operation.

More about this method from javadoc:
public void add(int index, E element): Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Example

In this example we have a LinkedList of Strings and we are adding an element to its 5th position(4th index) using add() method. The complete code is as follows:

import java.util.LinkedList;
import java.util.Iterator;
public class AddElement {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Delhi");
    linkedlist.add("Agra");
    linkedlist.add("Mysore");
    linkedlist.add("Chennai");
    linkedlist.add("Pune");
 
    // Adding new Element at 5th Position 
    linkedlist.add(4, "NEW ELEMENT");

    // Iterating the list in forward direction
    System.out.println("LinkedList elements After Addition:");
    Iterator it= linkedlist.iterator();
    while(it.hasNext()){
       System.out.println(it.next());
    }
 }
}

Output:



LinkedList elements After Addition:
Delhi
Agra
Mysore
Chennai
NEW ELEMENT
Pune

Java – Add elements at beginning and end of LinkedList example


Example

In this example we will learn how to add elements at the beginning and end of a LinkedList. We will be using addFirst() and addLast() method of LinkedList class. Method definition and description are as follows:

1) public void addFirst(E e): Inserts the specified element at the beginning of this list.
2) public void addLast(E e): Appends the specified element to the end of this list.

Complete Code:

import java.util.LinkedList;

public class AddExample {
 public static void main(String[] args) {
    // Creating LinkedList of String Elements
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    //Populating it with String values
    linkedlist.add("AA");
    linkedlist.add("BB");
    linkedlist.add("CC");
    linkedlist.add("DD");
    linkedlist.add("EE");

    //Displaying LinkedList elements
    System.out.println(linkedlist);
 
    //Adding an element at the beginning 
    linkedlist.addFirst("FIRST");
 
    //Displaying the List after addition
    System.out.println(linkedlist);
 
    //Adding an element at the end of list 
    linkedlist.addLast("LAST");
 
    //Displaying the final list
    System.out.println(linkedlist);
 }
}

Output:



[AA, BB, CC, DD, EE]
[FIRST, AA, BB, CC, DD, EE]
[FIRST, AA, BB, CC, DD, EE, LAST]

Adding element to front of LinkedList in Java


Description

Program to add element to front(head) of LinkedList.

Program

import java.util.LinkedList;
class LinkedListExample {

  public static void main(String[] args) {

     // create a LinkedList
     LinkedList<String> list = new LinkedList<String>();

     // Add elements
     list.add("AA");
     list.add("BB");
     list.add("CC");
     list.add("DD");
 
     // Display List element
     System.out.println("LinkedList Elements:"+list);

     // Adding element to front of LinkedList
     /* public boolean offerFirst(E e): Inserts the 
      * specified element at the front of this list.
      */
     list.offerFirst("NEW Element");

     // Displaying List after adding element
     System.out.println("LinkedList After Addition:"+list);
  }
}

Output:

LinkedList Elements:[AA, BB, CC, DD]
LinkedList After Addition:[NEW Element, AA, BB, CC, DD]

Java – Remove first and last element from LinkedList example

Example

We have used removeFirst() method to remove first and removeLast() method to remove last element from LinkedList. Method definition and description are as follows:

1) public E removeFirst(): Removes and returns the first element from this list.
2) public E removeLast(): Removes and returns the last element from this list.


Complete Code:

import java.util.LinkedList;
public class RemoveExample {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Item1");
    linkedlist.add("Item2");
    linkedlist.add("Item3");
    linkedlist.add("Item4");
    linkedlist.add("Item5");
 
    // Displaying Elements before remove
    System.out.println("LinkedList Elements are:");
    for(String str: linkedlist){
       System.out.println(str);
    }
 
    // Removing First element
    Object firstElement = linkedlist.removeFirst();
    System.out.println("\nElement removed: "+ firstElement);
 
    // Removing last Element
    Object lastElement = linkedlist.removeLast();
    System.out.println("Element removed: "+ lastElement);
 
    // LinkedList elements after remove
    System.out.println("\nList Elements after Remove:");
    for(String str2: linkedlist){
       System.out.println(str2);
    }
 }
}

Output:

LinkedList Elements are:
Item1
Item2
Item3
Item4
Item5

Element removed: Item1
Element removed: Item5

List Elements after Remove:
Item2
Item3
Item4

Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Java OOPS:Constructors in Java – A complete study!!

Java Basics:Data Types