-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIteratorDemo1.java
43 lines (35 loc) · 1.13 KB
/
IteratorDemo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Advances.CollectionDemo;
// https://www.youtube.com/watch?v=HA7LSr6-xls&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=525
/** Iterator demo 1 */
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
/**
* Iterator Demo1 -> collection iteration op, via Iterator interface
*
* <p>1) internal method : hasNext(), and next() 2) collection will return an Iterator obj when
* every call on iterator() (default idx is on the previous element) 3) remove() method can remove
* element within iteration -> NOTE : Iterator's remove() is DIFFERENT from default remove method
*/
public class IteratorDemo1 {
@Test
public void test1() {
/** remove() method in Iterator class */
Collection col1 = new ArrayList();
col1.add(123);
col1.add(456);
col1.add(789);
col1.add("yooooo");
col1.add("kate");
col1.add(new Person("kyo", 19));
Iterator iterator = col1.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if ("kate".equals(obj)) {
iterator.remove();
}
}
System.out.println(col1);
}
}