HashSet remove()
HashSet继承自Set接口的方法:boolean remove(Object o);
case1:
public class ShortTest { public static void main(String args[]) { Set<Short> set = new HashSet<>(); for(Short i=0; i<100; i++) {//自动拆箱:short(基本类型)--> Short(包装类型) set.add(i);//将i自动装箱后添加到set集合中 set.remove(i-1);//i-1位int类型,在remove(Object o)方法中自动包装为Integer
//System.out.println(b);//false,删除失败,不会影响set集合中元素 } System.out.println(set.size());//100 } }
case2:将case1中的Short改为Integer
public class ShortTest { public static void main(String args[]) { Set<Integer> set = new HashSet<>(); for(int i=0; i<100; i++) { set.add(i); boolean b = set.remove(i-1); System.out.println(b);//false(i=0) true(i=1) true...... } //System.out.println(set.size());//1 } }