LinkedHashSet retainAll(Collection) in Java

retainAll(Collection): This method is available in java.util.LinkedHashSet class of Java.

Syntax:

boolean java.util.AbstractCollection.retainAll(Collection<?> c)

This method retains only the elements in this collection that are contained in the specified collection.

Parameters: One parameter is required for this method.

c: a collection containing elements to be retained in this collection.

Returns: true if this collection changed as a result of the call.

Throws:

1. UnsupportedOperationException - if the retainAll operation is not supported by this collection.

2. ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection.

3. NullPointerException - if this collection contains one or more null elements and the specified collection does not permit null elements or if the specified collection is null.

Approach

Java

import java.util.LinkedHashSet;

public class LinkedHashSetretainAll {
    public static void main(String[] args) {

        LinkedHashSet<String> linkedHashSet =
new LinkedHashSet<>();

        linkedHashSet.add("Hello");
        linkedHashSet.add("Java");
        System.out.println(linkedHashSet.retainAll(
linkedHashSet));

    }
}

Output:

false


No comments:

Post a Comment