POP method java – Java LinkedList pop() Method with Examples

POP method java: In the previous article, we have discussed about Java LinkedList push() Method with Examples

In this article we are going to see the use of Java LinkedList pop() method in Java along with suitable examples.

Java LinkedList pop() Method with Examples

This java.util.LinkedList.pop() method is used to pop an element from the stack means here it simply deletes the element at the first(top/head) position of the linked list.

It returns no value i.e void.

Syntax:

LinkedListName.pop()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

 

Method-1: Java LinkedList pop() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now pop the top element in the LinkedList using pop( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("windows");
        l.add("linux");
        l.add("android");
        l.add("ios");
        l.add("symbian");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // pop the element from LinkedList
        l.pop();
        // Prints the LinkedList elements after pop() method
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [windows, linux, android, ios, symbian]
The new elements of LinkedList are: [linux, android, ios, symbian]

Method-2: Java LinkedList pop() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now pop the element in the LinkedList using pop( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(20);
        l.add(521);
        l.add(132);
        l.add(173);
        l.add(14);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // pop the element from LinkedList
        l.pop();
        // Prints the LinkedList elements after pop() method
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [20, 521, 132, 173, 14]
The new elements of LinkedList are: [521, 132, 173, 14]

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: