使用Iterator迭代器
- public class Demo {
- public static void main(String[] args) {
- Set<Object> obj = new HashSet<Object>();
- obj.add("a");
- obj.add("b");
- obj.add("c");
- System.out.println("移除前:" + obj.toString());
- Iterator<Object> it = obj.iterator();
- for(int i=0; i<obj.size(); i++){
- System.out.println(i);
- Object name = it.next();
- if("a".equals(name) || "b".equals(name)){
- it.remove();
- i--;
- }
- }
- System.out.println("移除后: " + obj.toString());
- }
- }