set循环遍历删除特定元素

set循环遍历删除特定元素

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());  
    }  
}  
原文地址:https://www.cnblogs.com/cchilei/p/13099192.html