I will leave a simple usage of Iterator. I haven't touched on Iterator until now, but it seems to be frequent, so I decided to summarize the basic usage.
Iterator
//List initialization
List<String> list = new ArrayList<>(Arrays.asList("apple","banana","orange","fish"));
//Declaration of Iterator
Iterator<String> iterator = list.iterator();
//Iterator method hasNext()Loop processing using
//hasNext(): Returns true if there are more elements in the iteration.
while(iterator.hasNext()) {
//Iterator method next()Take out the element with
//next(): Returns the next element in iterative processing.
String str = iterator.next();
if(str.equals("banana")) {
//Iterator method remove()Delete the element with
//Removes the last element returned by this iterator from the base collection
iterator.remove();
}
}
System.out.println(list);
[apple, orange, fish]
Recommended Posts