Как удалять из set java

What is the Set.remove() method in Java?

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

Overview

The Set.remove() method is present in the Set interface inside the java.util package. This method removes only the specified element from the Set .

Example

Let’s understand with the help of an example.

Let’s suppose that the Set contains the following: [1, 8, 5, 3, 0] . We execute the below statements on this set:

After using the remove() method, the Set contains [1, 5, 3] .

Syntax

public boolean remove(Object obj) 

Parameters

The Set.remove() has one parameter:

  • obj : It is a HashSet , TreeSet , or NavigableSet type Object that needs to be removed from the Set . As Set is an interface, it needs to be implemented by one of its implementing classes.

Return value

The Set.remove() method returns true if the Set gets changed.

Example

Let’s look at a code example below.

import java.util.Set;
import java.util.HashSet;
class Main
public static void main(String args[])
Set set1 = new HashSet();
set1.add(1);
set1.add(8);
set1.add(5);
set1.add(3);
set1.add(0);
System.out.println("set1 before calling remove(): "
+ set1);
boolean removeVal1;
boolean removeVal2;
removeVal1 = set1.remove(150);
removeVal2 = set1.remove(8);
System.out.println("set1 after calling remove(): "
+ set1);
System.out.println("the removal of 8 was: " + removeVal2);
System.out.println("the removal of 150 was: " + removeVal1);
>
>

Explanation

  • Line 8: We declare a Set of integer type: set1 . The object is created from the HashSet class. This is because the Set is an interface in Java and, hence, it cannot be instantiated.
  • Lines 9–13: We add the elements into the Set using the Set.add() method.
  • Line 15: We display the Set before calling the remove() method.
  • Lines 19–20: We call the remove() function to remove specific elements from the Set , and assign the returned values to boolean variables. When 150 passes as the argument, nothing happens. This is because it is not present. However, when 8 is removed, the change is reflected.
  • Line 21: We display the Set after calling the remove() method.
  • Lines 23–24: We display the returned values of the remove() method.

Learn in-demand tech skills in half the time

Источник

Interface Set

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2) , and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add , equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)

The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above).

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

Some set implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException . Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the set may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as «optional» in the specification for this interface.

Unmodifiable Sets

  • They are unmodifiable. Elements cannot be added or removed. Calling any mutator method on the Set will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the Set to behave inconsistently or its contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException .
  • They are serializable if all elements are serializable.
  • They reject duplicate elements at creation time. Duplicate elements passed to a static factory method result in IllegalArgumentException .
  • The iteration order of set elements is unspecified and is subject to change.
  • They are value-based. Programmers should treat instances that are equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones.
  • They are serialized as specified on the Serialized Form page.

This interface is a member of the Java Collections Framework.

Источник

What is the Set.remove() method in Java?

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

Overview

The Set.remove() method is present in the Set interface inside the java.util package. This method removes only the specified element from the Set .

Example

Let’s understand with the help of an example.

Let’s suppose that the Set contains the following: [1, 8, 5, 3, 0] . We execute the below statements on this set:

After using the remove() method, the Set contains [1, 5, 3] .

Syntax

public boolean remove(Object obj) 

Parameters

The Set.remove() has one parameter:

  • obj : It is a HashSet , TreeSet , or NavigableSet type Object that needs to be removed from the Set . As Set is an interface, it needs to be implemented by one of its implementing classes.

Return value

The Set.remove() method returns true if the Set gets changed.

Example

Let’s look at a code example below.

import java.util.Set;
import java.util.HashSet;
class Main
public static void main(String args[])
Set set1 = new HashSet();
set1.add(1);
set1.add(8);
set1.add(5);
set1.add(3);
set1.add(0);
System.out.println("set1 before calling remove(): "
+ set1);
boolean removeVal1;
boolean removeVal2;
removeVal1 = set1.remove(150);
removeVal2 = set1.remove(8);
System.out.println("set1 after calling remove(): "
+ set1);
System.out.println("the removal of 8 was: " + removeVal2);
System.out.println("the removal of 150 was: " + removeVal1);
>
>

Explanation

  • Line 8: We declare a Set of integer type: set1 . The object is created from the HashSet class. This is because the Set is an interface in Java and, hence, it cannot be instantiated.
  • Lines 9–13: We add the elements into the Set using the Set.add() method.
  • Line 15: We display the Set before calling the remove() method.
  • Lines 19–20: We call the remove() function to remove specific elements from the Set , and assign the returned values to boolean variables. When 150 passes as the argument, nothing happens. This is because it is not present. However, when 8 is removed, the change is reflected.
  • Line 21: We display the Set after calling the remove() method.
  • Lines 23–24: We display the returned values of the remove() method.

Learn in-demand tech skills in half the time

Источник

Interface Set

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2) , and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add , equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)

The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above).

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

Some set implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException . Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the set may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as «optional» in the specification for this interface.

Unmodifiable Sets

  • They are unmodifiable. Elements cannot be added or removed. Calling any mutator method on the Set will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the Set to behave inconsistently or its contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException .
  • They are serializable if all elements are serializable.
  • They reject duplicate elements at creation time. Duplicate elements passed to a static factory method result in IllegalArgumentException .
  • The iteration order of set elements is unspecified and is subject to change.
  • They are value-based. Programmers should treat instances that are equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones.
  • They are serialized as specified on the Serialized Form page.

This interface is a member of the Java Collections Framework.

Источник

Читайте также:  Idea no python interpreter configured
Оцените статью