|
|
Program: How to remove an element from collection using Iterator object?
Description: |
Below example shows how to remove an element from collection object using Iterator object.
The remove() method removes from the underlying collection the last element returned by the iterator
|
Code: |
package com.java2novice.iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MyItrRemoveElement {
public static void main(String a[]){
String removeElem = "Perl";
List<String> myList = new ArrayList<String>();
myList.add("Java");
myList.add("Unix");
myList.add("Oracle");
myList.add("C++");
myList.add("Perl");
System.out.println("Before remove:");
System.out.println(myList);
Iterator<String> itr = myList.iterator();
while(itr.hasNext()){
if(removeElem.equals(itr.next())){
itr.remove();
}
}
System.out.println("After remove:");
System.out.println(myList);
}
}
|
|
Output: |
Before remove:
[Java, Unix, Oracle, C++, Perl]
After remove:
[Java, Unix, Oracle, C++]
|
|
|
|
|
List Of Iterator Sample Programs:- How to iterate through collection objects?
- How to remove an element from collection using Iterator object?
|
|
|
What is System.out in Java
In System.out, out is an instance of PrintStream. It is a static member variable in
System class. This is called standard output stream, connected to console.
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles
|