Java Collections Framework:LinkedList-5

 

Java – Replace element in a LinkedList example

.

Example

The method we used in the below program is:
public E set(int index, E element): Replaces the element at the specified position in this list with the specified element.

Complete code:

import java.util.LinkedList;
public class ReplaceInLinkedList {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Cobol");
    linkedlist.add("JCL");
    linkedlist.add("C++");
    linkedlist.add("C#");
    linkedlist.add("Java");
 
    // Displaying Elements before replace
    System.out.println("Before Replace:");
    for(String str: linkedlist){
       System.out.println(str);
    }
 
    // Replacing 3rd Element with new value
    linkedlist.set(2, "NEW VALUE");
    System.out.println("\n3rd Element Replaced \n");

    // Displaying Elements after replace
    System.out.println("After Replace:");
    for(String str2: linkedlist){
       System.out.println(str2);
    }
 }
}

Output:



Before Replace:
Cobol
JCL
C++
C#
Java

3rd Element Replaced 

After Replace:
Cobol
JCL
NEW VALUE
C#
Java

Java – Check if a particular element exists in LinkedList example

public boolean contains(Object o): Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

import java.util.LinkedList;

public class CheckLinkedList {
 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");

    // contains() method checks whether the element exists
    if (linkedlist.contains("CC")) {
       System.out.println("Element CC is present in List");
    } else {
       System.out.println("List doesn't have element CC");
     }
    
    //Checking for element FF
    if (linkedlist.contains("FF")) {
       System.out.println("Element FF is present in List");
    } else {
        System.out.println("List doesn't have element FF");
      }
 }
}

Output:

Element CC is present in List
List doesn't have element FF

Clone a generic LinkedList in Java


Example

import java.util.LinkedList;
class LinkedListClone {

  public static void main(String[] args) {

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

     // Adding elements to the LinkedList
     list.add("Element1");
     list.add("Element2");
     list.add("Element3");
     list.add("Element4");

     // Displaying LinkedList elements
     System.out.println("LinkedList elements: "+list);
 
     // Creating another list
     LinkedList<String> list2 = new LinkedList<String>();
 
     // Clone list to list2
     /* public Object clone(): Returns a shallow copy of this
      * LinkedList. (The elements themselves are not cloned.)
      */
     list2 = (LinkedList)list.clone();
 
     // Displaying elements of second LinkedList
     System.out.println("List 2 Elements: "+list2);
  }
}

Output:

LinkedList elements: [Element1, Element2, Element3, Element4]
List 2 Elements: [Element1, Element2, Element3, Element4]

Java – Get the index of last occurrence of an element in LinkedList


Description

Program to find out the index of last occurrence of an element in 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("AA");
     list.add("DD");
     list.add("AA");
     list.add("EE");
 
     // Display LinkedList elements
     System.out.println("LinkedList elements: "+list);

     // get the index of last occurrence of element "AA"
     /* public int lastIndexOf(Object o): Returns the index 
      * of the last occurrence of the specified element in 
      * this list, or -1 if this list does not contain the 
      * element. 
      */
     System.out.println("LastIndex of AA:"+list.lastIndexOf("AA"));

     // get the index of last occurrence of element "ZZ"
     /* Note: The element ZZ does not exist in the list so
      * the method lastIndexOf would return -1 for it.
      */
     System.out.println("LastIndex of ZZ:"+list.lastIndexOf("ZZ"));
  }
}

Output:

LinkedList elements: [AA, BB, CC, AA, DD, AA, EE]
LastIndex of AA:5
LastIndex of ZZ:-1

LinkedList push() and pop() methods – Java


Description

Programs to demonstrate push and pop operations on LinkedList.

LinkedList.push(E e)

public void push(E e): Inserts the element at the front of the list.

Example:

import java.util.LinkedList;
class LinkedListExample {

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Jack");
     list.add("Robert");
     list.add("Chaitanya");
     list.add("kate");

     // Display LinkList elements
     System.out.println("LinkedList contains: "+list);

     // push Element the list
     list.push("NEW ELEMENT");

     // Display after push operation
     System.out.println("LinkedList contains: "+list);
  }
}

Output:



LinkedList contains: [Jack, Robert, Chaitanya, kate]
LinkedList contains: [NEW ELEMENT, Jack, Robert, Chaitanya, kate]

LinkedList.pop()

public E pop(): Removes and returns the first element of the list.

Example:

import java.util.LinkedList;
class LinkedListPopDemo{

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Jack");
     list.add("Robert");
     list.add("Chaitanya");
     list.add("kate");

     // Display LinkList elements
     System.out.println("LinkedList before: "+list);

     // pop Element from list and display it
     System.out.println("Element removed: "+list.pop());

     // Display after pop operation
     System.out.println("LinkedList after: "+list);
  }
}

Output:

LinkedList before: [Jack, Robert, Chaitanya, kate]
Element removed: Jack
LinkedList after: [Robert, Chaitanya, kate]

Java – LinkedList poll(), pollFirst() and pollLast() methods


Description

Example Programs for poll(), pollFirst() and pollLast() methods of LinkedList class.

LinkedList.poll()

Retrieves and removes the head (first element) of this list.

import java.util.LinkedList;
class LinkedListPollMethod{

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Element1");
     list.add("Element2");
     list.add("Element3");
     list.add("Element4");

     // Display LinkList elements
     System.out.println("LinkedList before: "+list);

     /* poll(): Retrieves and removes the head (first element)
      * of this list.
      */
     System.out.println("Element removed: "+list.poll());

     // Displaying list elements after poll() operation
     System.out.println("LinkedList after: "+list);
  }
}

Output:

LinkedList before: [Element1, Element2, Element3, Element4]
Element removed: Element1
LinkedList after: [Element2, Element3, Element4]

LinkedList.pollFirst()

public E pollFirst(): Retrieves and removes the first element of this list, or returns null if this list is empty.



import java.util.LinkedList;
class LinkedListPollFirstDemo{

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Element1");
     list.add("Element2");
     list.add("Element3");
     list.add("Element4");

     // Display LinkList elements
     System.out.println("LinkedList before: "+list);

     /* pollFirst(): Retrieves and removes the first element 
      * of this list, or returns null if this list is empty.
      */
     System.out.println("Element removed: "+list.pollFirst());

     // Display list after calling pollFirst() method
     System.out.println("LinkedList after: "+list);
  }
}

Output:

LinkedList before: [Element1, Element2, Element3, Element4]
Element removed: Element1
LinkedList after: [Element2, Element3, Element4]

LinkedList.pollLast()

public E pollLast(): Retrieves and removes the last element of this list, or returns null if this list is empty.

import java.util.LinkedList;
class LinkedListPollLastDemo{

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Element1");
     list.add("Element2");
     list.add("Element3");
     list.add("Element4");

     // Display LinkList elements
     System.out.println("LinkedList before: "+list);

     /* pollFirst(): Retrieves and removes the first element 
      * of this list, or returns null if this list is empty.
      */
     System.out.println("Element removed: "+list.pollLast());

     // Display after calling pollLast() method
     System.out.println("LinkedList after: "+list);
  }
}

Output:

LinkedList before: [Element1, Element2, Element3, Element4]
Element removed: Element4
LinkedList after: [Element1, Element2, Element3]

Java – LinkedList peek(), peekFirst() and peekLast() methods


Description

public E peek(): Retrieves, but does not remove, the head (first element) of this list.

public E peekFirst(): Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

public E peekLast(): Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;
class LinkedListPeekDemo{

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Element1");
     list.add("Element2");
     list.add("Element3");
     list.add("Element4");

     // Display LinkList elements
     System.out.println("LinkedList before: "+list);

     //peek()
     System.out.println(list.peek());
 
     //peekFirst()
     System.out.println(list.peekFirst());
 
     //peekLast()
     System.out.println(list.peekLast());

     // Should be same as peek methods does not remove
     System.out.println("LinkedList after: "+list);
  }
}

Output:



LinkedList before: [Element1, Element2, Element3, Element4]
Element1
Element1
Element4
LinkedList after: [Element1, Element2, Element3, Element4]

Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

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

Java Basics:Data Types