How To Take Away Objects From Collection Or Arraylist Inwards Coffee Spell Traversing - Iterator Remove() Method Example
How create you lot take objects from Java collections similar ArrayList, while iterating is 1 of the frequent questions my reader asked to me inwards my ship service almost Top 25 Java Collection Interview Questions. Well, this inquiry may seems quite easy, because every java.util.Collection implementation e.g. List or Set has remove() method to delete a detail object, which tin survive used to take elements from whatsoever Collection e.g. ArrayList, LinkedList or Vector. Well, this is where things goes incorrect together with interviewers are interested to see, whether you lot tin betoken almost remove() method from Iterator or not.
Answer of this inquiry is every bit uncomplicated every bit that, you lot should survive using Iterator's remove() method to delete whatsoever object from Collection you are iterating, but this is non the halt of this question.
Most probable you lot volition survive asked to explain, what is divergence inwards removing object using remove() method of Collection over remove() method of Iterator together with why 1 should locomote over other?
Reason is ConcurrentModificationException, if you lot use remove() method of List, Set or basically from whatsoever Collection to delete object piece iterating, it volition throw ConcurrentModificationException.
Though remove() method of java.util.Collection plant fine to take private object, they don't piece of occupation well, when you lot are iterating over collection. Let's run into a code event to clear doubts
to survive frank, fifty-fifty alongside the locomote of modern IDE similar Eclipse, you lot may code it wrong, together with halt upwards confusing yourself when you lot run into ConcurrentModificationException, because that old mislead programmers. It looks that may survive about other thread is modifying collection, together with alongside this idea you lot won't await the code you lot are using for traversing ArrayList. That's why old interviewer acquaint code together with inquire you lot to honor bugs on it, or they may only inquire you lot to write code for removing objects from Collection piece traversing over them. Always recall to locomote Iterator's remove() method for removing objects from Collection inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
tutorial)
How to variety an ArrayList inwards ascending together with descending social club inwards Java? (tutorial)
What is the divergence betwixt ArrayList together with HashSet inwards Java? (answer)
What is the divergence betwixt TreeMap together with TreeSet inwards Java? (answer)
What is the divergence betwixt HashMap together with ConcurrentHashMap inwards Java? (answer)
The divergence betwixt HashSet together with TreeSet inwards Java? (answer)
The divergence betwixt ArrayList together with LinkedList inwards Java? (answer)
The divergence betwixt Vector together with ArrayList inwards Java? (answer)
Thanks for reading this article hence far. If you lot similar this article together with then delight part alongside your friends together with colleagues. If you lot bring whatsoever questions or feedback together with then delight drib a comment.
Answer of this inquiry is every bit uncomplicated every bit that, you lot should survive using Iterator's remove() method to delete whatsoever object from Collection you are iterating, but this is non the halt of this question.
Most probable you lot volition survive asked to explain, what is divergence inwards removing object using remove() method of Collection over remove() method of Iterator together with why 1 should locomote over other?
Reason is ConcurrentModificationException, if you lot use remove() method of List, Set or basically from whatsoever Collection to delete object piece iterating, it volition throw ConcurrentModificationException.
Though remove() method of java.util.Collection plant fine to take private object, they don't piece of occupation well, when you lot are iterating over collection. Let's run into a code event to clear doubts
How to Remove elements From ArrayList piece Iterating
remove() method of ArrayList to take objects, instead of Iterator's take method. In social club to develop ConcurrentModificationException, exactly locomote remove() method of java.util.Iteator class. import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Java programme to demonstrate how to take object shape List together with differnece
* betwixt Iterator's remove() method together with Colection's remove() method inwards Java
*
* @author http://javarevisited.blogspot.com
*/
public class ObjectRemovalTest {
public static void main(String args[]) {
List 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();
while(itr.hasNext()){
StockExchange telephone substitution = itr.next();
if(exchange.isClosed()){
markets.remove(exchange); //Use itr.remove() method
}
}
}
}
interface StockExchange{
public boolean isClosed();
}
Output
Exception inwards thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at ObjectRemovalTest.main(StringReplace.java:63)
Java Result: 1
to survive frank, fifty-fifty alongside the locomote of modern IDE similar Eclipse, you lot may code it wrong, together with halt upwards confusing yourself when you lot run into ConcurrentModificationException, because that old mislead programmers. It looks that may survive about other thread is modifying collection, together with alongside this idea you lot won't await the code you lot are using for traversing ArrayList. That's why old interviewer acquaint code together with inquire you lot to honor bugs on it, or they may only inquire you lot to write code for removing objects from Collection piece traversing over them. Always recall to locomote Iterator's remove() method for removing objects from Collection inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
tutorial)
How to variety an ArrayList inwards ascending together with descending social club inwards Java? (tutorial)
What is the divergence betwixt ArrayList together with HashSet inwards Java? (answer)
What is the divergence betwixt TreeMap together with TreeSet inwards Java? (answer)
What is the divergence betwixt HashMap together with ConcurrentHashMap inwards Java? (answer)
The divergence betwixt HashSet together with TreeSet inwards Java? (answer)
The divergence betwixt ArrayList together with LinkedList inwards Java? (answer)
The divergence betwixt Vector together with ArrayList inwards Java? (answer)
Thanks for reading this article hence far. If you lot similar this article together with then delight part alongside your friends together with colleagues. If you lot bring whatsoever questions or feedback together with then delight drib a comment.
0 Response to "How To Take Away Objects From Collection Or Arraylist Inwards Coffee Spell Traversing - Iterator Remove() Method Example"
Post a Comment