📜  如何从 LinkedHashSet 中删除用户定义的对象?

📅  最后修改于: 2022-05-13 01:54:32.513000             🧑  作者: Mango

如何从 LinkedHashSet 中删除用户定义的对象?

LinkedHashSet是 HashSet 的有序版本,它在所有元素之间维护一个双向链表。当需要维护迭代顺序时,使用这个类。当遍历 HashSet 时,顺序是不可预测的,而 LinkedHashSet 允许我们按照元素插入的顺序遍历元素。

例子:

Input: ["Geeks", "for", "geeks"]
DELETE = "geeks"
Output: "Geeks"
    "for"

Input: [1, 2, 3, 4, 5]
DELETE = 2
Output: [1, 3, 4, 5]

我们可以通过两种方式从 LinkedHashSet 中删除元素:

  1. 使用 remove() 方法
  2. 使用 clear() 方法

方法 1: remove( )方法用于从 LinkedHashSet 对象中删除特定元素。

句法:

LinkedHashSet.remove(Object O)

参数:参数O是LinkedHashSet的类型,指定要从LinkedHashSet中移除的元素。

返回值:如果指定的元素存在于 LinkedHashSet 中,则此方法返回 True,否则返回 False。

Java
// Java program  to delete user defined 
// objects from LinkedHashSet
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        LinkedHashSet lset
            = new LinkedHashSet();
  
        // add elements to HashSet
        lset.add("GEEKS");
        lset.add("FOR");
        lset.add("GEEKS");
  
        // Displaying the LinkedHashSet Before Deleting
        System.out.println("Before deleting an element :");
        System.out.println(lset);
  
        // remove() method to delete an element from
        // LinkedHashSet
        lset.remove("FOR");
  
        // Displaying the LinkedHashSet After Deleting
        System.out.println("After deleting an element:");
        System.out.println(lset);
    }
}


Java
// Java program to delete elements
// from LinkedHashSet
  
import java.util.*;
import java.util.LinkedHashSet;
  
public class GFG {
    public static void main(String[] args)
    {
  
        LinkedHashSet l_set
            = new LinkedHashSet();
  
        // add elements to HashSet
        l_set.add("GEEKS");
        l_set.add("FOR");
        l_set.add("GEEKS");
  
        // Displaying the LinkedHashSet Before Deleting
        System.out.println("Before deleting an element :");
        System.out.println(l_set);
  
        // clear() method to delete all element from
        // LinkedHashSet
        l_set.clear();
  
        // Displaying an empty LinkedHashSet After Deleting
        System.out.println("After deleting all element:");
        System.out.println(l_set);
    }
}


输出
Before deleting an element :
[GEEKS, FOR]
After deleting an element:
[GEEKS]

方法二: clear() 方法用于删除 LinkedHashSet 中的所有元素。此调用返回后,该集合将为空。

句法:

public void clear()

返回值:此方法不返回任何内容。

下面是说明clear()方法的示例。

Java

// Java program to delete elements
// from LinkedHashSet
  
import java.util.*;
import java.util.LinkedHashSet;
  
public class GFG {
    public static void main(String[] args)
    {
  
        LinkedHashSet l_set
            = new LinkedHashSet();
  
        // add elements to HashSet
        l_set.add("GEEKS");
        l_set.add("FOR");
        l_set.add("GEEKS");
  
        // Displaying the LinkedHashSet Before Deleting
        System.out.println("Before deleting an element :");
        System.out.println(l_set);
  
        // clear() method to delete all element from
        // LinkedHashSet
        l_set.clear();
  
        // Displaying an empty LinkedHashSet After Deleting
        System.out.println("After deleting all element:");
        System.out.println(l_set);
    }
}
输出
Before deleting an element :
[GEEKS, FOR]
After deleting all element:
[]