The “iterator” technique presents a protected and simple approach to take away or delete particular knowledge components from a group whereas iterating over it. It prevents the incidence of “ConcurrentModificationException” which arises when the improved for loop tries to change the gathering. It shops the observe of the present place and presents programmers the prospect to maneuver ahead and take away the underlying components as wanted.
This weblog demonstrates the method to take away a component from a group by using the iterator technique.
The best way to Use the Iterator Technique to Take away an Factor From a Assortment in Java?
The iterator technique is unidirectional and can be utilized with numerous assortment varieties, together with “ArrayList”, “LinkedList”, and so on. It presents a uniform approach to take away components from any assortment that implements the “Iterable” interface.
Allow us to go to some examples to raised perceive the iterator technique:
Instance 1: Eradicating Particular Parts From the Assortment Utilizing Iterator Technique
The iterator technique could be utilized with “if” statements or loops to pick out the desired components. After which the “take away()” technique is used to carry out the deletion operation as proven under:
class IterExam
{
public static void fundamental(String[] args)
{
ArrayList<Integer> demoArrLis = new ArrayList<Integer>();
for(int i=0;i<=20;i=i+2)
{
demoArrLis.add(i);
}
Iterator<Integer> itr = demoArrLis.iterator();
System.out.println(“Offered ArrayList:”);
for(int i=0;i<demoArrLis.measurement();i++)
{
System.out.print(demoArrLis.get(i)+” “);
}
whereas(itr.hasNext())
{
if(itr.subsequent()%3==0)
itr.take away();
}
System.out.println(“nAfter Eradicating Odd ArrayList Parts”);
for(int i=0;i<demoArrLis.measurement();i++)
{
System.out.print(demoArrLis.get(i)+” “);
}
}
}
Description of the above code:
- First, the ArrayList is created after which a a number of of “2” is inserted until the vary of “20” with the assistance of the “for” loop.
- Subsequent, declare an “iterator” technique object to show the weather of ArrayList on the console.
- Then, make the most of the “hasNext()” technique with the iterator object to traverse via all residing ArrayList components.
- After that, the “if” assertion is utilized to test the weather which might be absolutely divisible by “3” inside it.
- Then, the “take away()” technique is used to delete the weather which might be returned by the “if” assertion.
- In the long run, the up to date ArrayList has been displayed utilizing the “for” loop.
After the compilation:
The output shows the particular components, that are divisible by three and faraway from the ArrayList utilizing an iterator technique.
Instance 2: Eradicating All Parts From the Assortment
To take away all residing components of the gathering, the iterator technique can be utilized together with its “take away()” technique as proven under:
class Vacancy
{
public static void fundamental(String arg[])
{
Vector<Integer> testVector=new Vector<Integer>();
testVector.add(18);
testVector.add(21);
testVector.add(25);
testVector.add(27);
testVector.add(30);
Iterator<Integer> traverse = testVector.iterator();
System.out.print(“Current components:”);
whereas(traverse.hasNext())
System.out.print(traverse.subsequent()+” “);
System.out.print(“nThe testVector accommodates “+testVector.measurement()+” components “);
traverse = testVector.iterator();
whereas(traverse.hasNext())
{
System.out.print(“neradicating “+traverse.subsequent());
traverse.take away();
}
System.out.println(“nNow, the testVector accommodates “+testVector.measurement()+” components”);
}
}
Description of code:
- First, declare and initialize “Vector” with dummy integer kind values utilizing the “add()” technique.
- Subsequent, create an iterator technique that makes use of the “hasNext()” and “subsequent()” strategies. It shows the residing knowledge components and their corresponding measurement.
- Then, make the most of the “hasNext()” property alongside the iterator object contained in the “whereas” loop.
- After that, make the most of the “subsequent()” technique to pick out the upcoming component, after which, the take away() technique is known as to delete every receiving component.
- On this means, all components of the Vector are eliminated and the scale of the Vector is displayed on the console.
Description of code:
The above code confirms that every one components from the gathering have been eliminated.
Instance 3: Eradicating Factor Utilizing ListIterator
The ListIterator works equally to the iterator technique. The ListIterator performs traversing on each side within the ahead and backward instructions. To take away particular components from the gathering utilizing the ListIterator in Java, go to the under code:
import java.util.ListIterator;
public class ListIteratorDemo {
//Initializing the principle() technique
public static void fundamental(String[] args)
{ //Declaring and Initializing the ArrayList
ArrayList<String> alist = new ArrayList<String>();
alist.add(“Hoodie”);
alist.add(“Polo”);
alist.add(“Henleys”);
alist.add(“Sweat”);
alist.add(“Informal”);
alist.add(“Luxurious”);
alist.add(“Dryfit”);
ListIterator<String> litr = alist.listIterator();
System.out.println(“Record earlier than removing”);
for (int i = 0; i < alist.measurement(); i++)
System.out.print(alist.get(i) + ” “);
whereas (litr.hasNext()) {
if (litr.subsequent().equals(“Sweat”)) {
litr.take away();
}
}
System.out.println(“nRecord after removing”);
for (int i = 0; i < alist.measurement(); i++)
System.out.print(alist.get(i) + ” “);
}
}
Description of the above code:
- First, declare and initialize the ArrayList with the dummy String kind values and show all components on the console utilizing the “for” loop.
- Subsequent, create an object which calls the “hasNext()” technique contained in the “whereas” loop. It traverses via all residing components.
- Then, the “if” assertion is used that checks every component with particular textual content, when that component matches the “take away()” technique is known as. It deletes the particular component from the “aList” named ArrayList.
- In the long run, show the modified ArrayList on the console.
After compilation:
The snapshot confirms the desired knowledge component has been faraway from the gathering utilizing ListIterator.
Conclusion
To take away the component from a group, the “take away()” technique of the iterator is utilized. The iterator traverses via the ArrayList to seek out the focused knowledge. As soon as it’s discovered the “take away()” technique is used to take away that particular knowledge component. It presents a standardized method throughout totally different collections and prevents points and the incidence of a number of exceptions like “ConcurrentModificationException”. Together with it, the “ListIterator” can be useful.