Home » Java programming language

How to remove an entry from a Collection in Java?

Removing an entry/element from Collection: Here, we are going to learn how to remove an entry from a Collection in Java programing language? We are also learning about the differences between the remove() method of Collection interface and remove() method of Iterator interface?
Submitted by Preeti Jain, on August 04, 2019

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
    2. By using remove(int index) method of List
    3. By using remove() method of Iterator
  • Collection interface adds a method remove(Object obj) is used to remove a specified element from a Collection.
  • List interface adds another remove(int index) method is used to remove an object at the specified index in the method.
  • Iterator interface also adds remove() method is used to remove the current object from a Collection.

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

remove(Object obj) method of Collection interface

  • This method is available in java.util package.
  • The remove() method of Collection is used to remove a single object of the specified element from a Collection.
  • The syntax of remove() method of Collection interface is given below:
    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.
  • 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].
  • 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.

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?

remove() method of Iterator interface

  • This method is available in java.util package.
  • remove() method of Iterator removes the current object or element in the Collection.
  • In case of remove() method, we can't remove the specified element or random element in the middle directly but without iteration.
  • The syntax of this remove() method of Iterator interface is given below:
    void remove(){}
    
  • The return type of remove() method is void so it does not return anything.
  • This method throws an exception IllegalStateException if this method is called before next() method is called.
  • 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.

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]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.