标签:构造 res public void obj map nal strong oid
元素没有顺序,不可以重复
Set容器类有HashSet和TreeSet
package TestCollection; import java.util.HashSet; import java.util.Set; public class TestSet { public static void main(String[] args) { Set set=new HashSet(); //add set.add("aaa"); set.add("bbb"); set.add(new String("aaa")); System.out.println(set.size()); //2 //remove set.remove("bbb"); System.out.println(set.size()); //2 } }
package TestCollection; import java.util.HashMap; public class MySet { int size; HashMap map; private static final Object PRESENT=new Object(); public int size() { return size; } //构造器 public MySet() { map=new HashMap(); } public void add(Object o) { map.put(o,PRESENT);//set的不可重复就是利用了map键对象的不可重复 size++; } public static void main(String[] args) { MySet set=new MySet(); set.add("aaa"); System.out.println(set.size()); } }
标签:构造 res public void obj map nal strong oid
原文地址:https://www.cnblogs.com/hapyygril/p/12497442.html