Friday, April 19, 2024
HomeJavaLearn how to Take away Objects from Assortment or Checklist in Java?...

Learn how to Take away Objects from Assortment or Checklist in Java? Iterator take away() technique Instance


How do you take away objects from Java collections like ArrayList, whereas iterating is likely one of the frequent questions my reader requested me in my publish about Prime 25 Java Assortment Interview Questions. Effectively, this query could seem fairly simple, due to each java.util.Assortment implementation like Checklist or Set has take away() technique to delete a specific object, which can be utilized to take away components from any Assortment like ArrayList, LinkedList, or Vector. Effectively, that is the place issues go incorrect and interviewers have an interest to see, whether or not you possibly can level out the take away() technique from Iterator or not.

The reply to this query is so simple as that, you have to be utilizing
Iterator’s take away() technique to delete any object from Assortment you’re iterating, however this isn’t the top of this query.

Almost certainly you may be requested to clarify, what’s a distinction in eradicating objects utilizing the take away() technique of Assortment over take away() technique of Iterator and why one ought to use over the opposite?

The reason being ConcurrentModificationException, should you use the take away() technique of Checklist, Set, or principally from any Assortment to delete object whereas iterating, it would throw ConcurrentModificationException. 


Although take away() technique of java.util.Assortment works superb to take away particular person objects, they do not work properly if you find yourself iterating over a set.  Let’s have a look at a code instance to clear doubts

And, If you’re new to the Java world then I additionally advocate you undergo The Full Java MasterClass on Udemy to study Java in a greater and extra structured means. This is likely one of the finest and up-to-date programs to study Java on-line.

Learn how to Take away components From ArrayList whereas Iterating? Instance

Within the beneath code, we’ve got a listing of exchanges and we’re eradicating exchanges which might be closed in the mean time. Code is so simple as it could possibly be and it is arduous to seek out something incorrect with it by simply trying, however issues will probably be totally different if you run it.


You’ll be hit by ConcurrentModificationException, as quickly as you run, as a result of listed here are we’re utilizing the take away() technique of ArrayList to take away objects, as a substitute of the Iterator’s take away technique. To be able to repair ConcurrentModificationException, simply use the take away() technique of java.util.Iteator class.
And, right here is our full Java program to check and ensure this habits. This program demonstrates that should you use the Collections take away technique whereas traversing over Assortment utilizing Iterator to take away the present ingredient then you will get  ConcurrentModificationException and through the use of Iterator’s take away technique you possibly can simply keep away from it. 

import java.util.ArrayList;
import java.util.Iterator;
  * Java program to display methods to take away the item from Checklist and distinction
  * between Iterator’s take away() technique and Assortment’s take away() technique in Java
public class ObjectRemovalTest {
    public static void fundamental(String args[]) {
       Checklist markets = new ArrayList();
       StockExchange TSE = new StockExchange(){
            @Override
            public boolean isClosed() {
                return true;
            }         
       StockExchange HKSE = new StockExchange(){
            @Override
            public boolean isClosed() {
                return true;
            }         
       StockExchange NYSE = new StockExchange(){
            @Override
            public boolean isClosed() {
                return false;
            }         
       markets.add(TSE);
       markets.add(HKSE);
       markets.add(NYSE);
       Iterator itr = markets.iterator();
       whereas(itr.hasNext()){
           StockExchange alternate = itr.subsequent();
           if(alternate.isClosed()){
               markets.take away(alternate); //Use itr.take away() technique
    public boolean isClosed();
Exception in thread “fundamental” java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
        at java.util.AbstractList$Itr.subsequent(AbstractList.java:343)
        at ObjectRemovalTest.fundamental(StringReplace.java:63)
To be frank, even with the usage of trendy IDE like Eclipse, you might code is incorrect, and find yourself complicated your self if you see ConcurrentModificationException as a result of that generally mislead programmers.
It seems which may be one other thread is modifying the gathering, and with this thought, you will not have a look at the code you’re utilizing for traversing ArrayList. If you wish to study extra about Collections in Java then I additionally recommend you take a look at these Java Collections and Stream Programs, the place I’ve shared the perfect programs to study this essential framework. 
How to Remove Objects from Collection or List in Java? Iterator remove() method Example

That is why generally interviewers current the code and asks you to seek out bugs on it, or they might merely ask you to jot down code for eradicating objects from Assortment whereas traversing over them. All the time bear in mind to make use of Iterator’s take away() technique for eradicating objects from Assortment in Java.

Different Java Assortment tutorials you might like

  • Learn how to kind a Map by keys and values in Java? (tutorial)
  • Learn how to kind an ArrayList in ascending and descending order in Java? (tutorial)
  • What’s the distinction between ArrayList and HashSet in Java? (reply)
  • What’s the distinction between TreeMap and TreeSet in Java? (reply)
  • What’s the distinction between HashMap and ConcurrentHashMap in Java? (reply)
  • The distinction between HashSet and TreeSet in Java? (reply)
  • The distinction between ArrayList and LinkedList in Java? (reply)
  • The distinction between Vector and ArrayList in Java? (reply)

Thanks for studying this text to date. If you happen to like this text then please share it with your folks and colleagues. When you have any questions or suggestions then please drop a remark.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments