A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/blockingdeque-removelastoccurrence-method-in-java-with-examples/ below:

BlockingDeque removeLastOccurrence() method in Java with Examples

BlockingDeque removeLastOccurrence() method in Java with Examples

Last Updated : 14 Oct, 2019

The

removeLastOccurrence()

method of

BlockingDeque

removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it remains unchanged. It returns true if this deque contained the specified element, else it returns false.

Syntax:
public boolean removeLastOccurrence(Object o)
Parameters:

This method accepts a mandatory parameter

o

which specifies the element whose last occurrence is to be removed from the Dequeue container.

Returns:

This method returns

true

if the element is present and removed from the Deque container, else it returns

false

.

Note

: The

removeLastOccurrence()

method of

BlockingDeque

has been inherited from the LinkedBlockingDeque class in Java. Below programs illustrate removeLastOccurrence() method of BlockingDeque:

Program 1:

When element is present.

Java
// Java Program to demonstrate removeLastOccurrence()
// method of BlockingDeque

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;

public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {

        // create object of BlockingDeque
        BlockingDeque<Integer> BD
            = new LinkedBlockingDeque<Integer>();

        // Add numbers to end of BlockingDeque
        BD.add(15);
        BD.add(20);
        BD.add(20);
        BD.add(15);

        // print Dequeue
        System.out.println("Blocking Deque: " + BD);

        if (BD.removeLastOccurrence(15))
            System.out.println("Last occurrence of 15 removed");
        else
            System.out.println("15 not present and not removed");

        // prints the Deque after removal
        System.out.println("Blocking Deque: " + BD);
    }
}
Output:
Blocking Deque: [15, 20, 20, 15]
Last occurrence of 15 removed
Blocking Deque: [15, 20, 20]
Program 2:

When element is not present.

Java
// Java Program to demonstrate removeLastOccurrence()
// method of BlockingDeque

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;

public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {

        // create object of BlockingDeque
        BlockingDeque<Integer> BD
            = new LinkedBlockingDeque<Integer>();

        // Add numbers to end of BlockingDeque
        BD.add(15);
        BD.add(20);
        BD.add(20);
        BD.add(15);

        // print Deque
        System.out.println("Blocking Deque: " + BD);

        if (BD.removeLastOccurrence(10))
            System.out.println("Last occurrence of 10 removed");
        else
            System.out.println("10 not present and not removed");

        // prints the Deque after removal
        System.out.println("Blocking Deque: " + BD);
    }
}
Output:
Blocking Deque: [15, 20, 20, 15]
10 not present and not removed
Blocking Deque: [15, 20, 20, 15]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#removeLastOccurrence(java.lang.Object)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4