ldap新增条目java_如何从Java中的集合中删除条目?

ldap新增条目java

从集合中删除条目 (Removing an entry from a Collection)

  • As we know, we can remove an entry from a Collection in three ways.

    众所周知,我们可以通过三种方式从集合删除条目

  1. By using remove(Object obj) method of Collection

    通过使用Collection的remove(Object obj)方法

  2. By using remove(int index) method of List

    通过使用List的remove(int index)方法

  3. By using remove() method of Iterator

    通过使用Iterator的remove()方法

  • Collection interface adds a method remove(Object obj) is used to remove a specified element from a Collection.

    集合接口添加了方法remove(Object obj),该方法用于从集合中删除指定的元素。

  • List interface adds another remove(int index) method is used to remove an object at the specified index in the method.

    列表接口添加了另一个remove(int index)方法,该方法用于删除该方法中指定索引处的对象。

  • Iterator interface also adds remove() method is used to remove the current object from a Collection.

    迭代器接口还添加了remove()方法,该方法用于从Collection中删除当前对象。

  • Now, we will see how remove(Object obj) method of Collection differs from remove() method of Iterator?

    现在,我们将看到Collection的remove(Object obj)方法与Iterator的remove()方法有何不同?

    Collection接口的remove(Object obj)方法 (remove(Object obj) method of Collection interface)

    • This method is available in java.util package.

      此方法在java.util包中可用。

    • The remove() method of Collection is used to remove a single object of the specified element from a Collection.

      Collection的remove()方法用于从Collection中移除指定元素的单个对象。

    • The syntax of remove() method of Collection interface is given below:

      下面给出Collection接口的remove()方法的语法:

      boolean remove(Object obj)
      
      
    • The return type of this method is boolean so it returns true if the element or object is removed successfully else return false.

      此方法的返回类型为boolean,因此如果成功删除元素或对象,则返回true,否则返回false。

    • This method throws an exception [NullPointerException] if we pass null as a parameter in the method of Collection and if we pass other type elements as a parameter in the method of Collection then also we will get an exception[ClassCastException].

      如果我们在Collection方法中将null作为参数传递,并且在Collection方法中将其他类型元素作为参数传递,则此方法将引发异常[ NullPointerException ],我们还将获得异常[ ClassCastException ]。

    • When we iterate, Let suppose we are traversing a list and remove only a few elements based on logic and if we use Collection remove() method then we will get an exception ConcurrentModificationException.

      进行迭代时,假设我们遍历一个列表并仅基于逻辑删除一些元素,并且如果我们使用Collection remove()方法 ,则将获得一个ConcurrentModificationException异常。

    Example:

    例:

    // Java program to demonstrate the behavior of 
    // remove() method of Collection
    
    import java.util.*;
    
    class RemoveCollectionMethod {
        public static void main(String[] args) {
            Collection collection = new LinkedList();
    
            collection.add(10);
            collection.add(20);
            collection.add(30);
            collection.add(40);
            collection.add(50);
    
            System.out.println("Current LinkedList:" + collection);
    
            collection.remove(30);
    
            System.out.println(" LinkedList:" + collection);
        }
    }
    
    

    Output

    输出量

    E:\Programs>javac RemoveCollectionMethod.java
    
    E:\Programs>java RemoveCollectionMethod
    Current LinkedList:[10, 20, 30, 40, 50]
     LinkedList:[10, 20, 40, 50]
    
    

    Now, we will see how remove() method of Iterator differs from remove(Object obj) method of Collection?

    现在,我们将看到Iterator的remove()方法与Collection的remove(Object obj)方法有何不同?

    Iterator接口的remove()方法 (remove() method of Iterator interface)

    • This method is available in java.util package.

      此方法在java.util包中可用。

    • remove() method of Iterator removes the current object or element in the Collection.

      Iterator的remove()方法删除Collection中的当前对象或元素。

    • In case of remove() method, we can't remove the specified element or random element in the middle directly but without iteration.

      如果使用remove()方法 ,则不能直接在中间移除指定的元素或随机元素,而不能进行迭代。

    • The syntax of this remove() method of Iterator interface is given below:

      Iterator接口的remove()方法的语法如下:

      void remove(){}
      
      
    • The return type of remove() method is void so it does not return anything.

      remove()方法的返回类型为void,因此它不返回任何内容。

    • This method throws an exception IllegalStateException if this method is called before next() method is called.

      如果在调用next()方法之前调用此方法,则此方法将引发异常IllegalStateException 。

    • When we iterate, Let suppose we are traversing a list and remove only a few elements based on logic and if we use Iterator remove() method then we will not get any exception there.

      当我们进行迭代时,假设我们遍历一个列表并仅基于逻辑删除一些元素,并且如果我们使用Iterator remove()方法,那么那里不会有任何异常。

    Example:

    例:

    // Java program to demonstrate the behavior of 
    // remove() method of Iterator
    
    import java.util.*;
    
    class RemoveIteratorMethod {
        public static void main(String[] args) {
            // Creating a Set interface object
            Set set = new HashSet();
    
            // By using add() method to add few elements in set
            set.add("Java");
            set.add("Python");
            set.add("C++");
            set.add("Ruby");
            set.add("C");
    
            // Creating an object of Iterator interface
            Iterator itr = set.iterator();
    
            // loop for traversing an element
            while (itr.hasNext()) {
                String str = (String) itr.next();
                if (str.equals("C"))
                    itr.remove();
            }
            // Display elements of Set interface 
            System.out.println("The final list of Iterator :" + set);
        }
    }
    
    

    Output

    输出量

    E:\Programs>javac RemoveIteratorMethod.java
    
    E:\Programs>java RemoveIteratorMethod
    The final list of Iterator :[Ruby, Python, C++, Java]
    
    
    

    翻译自: https://www.includehelp.com/java/how-to-remove-an-entry-from-a-collection-in-java.aspx

    ldap新增条目java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值