java中的Set集合和List集合一样都继承Collection的接口,Set集合是一个不包含重复元素的集合,主要包含三种存放数据类型的变量,分别是HashSet、LinkedHashSet和TreeSet,和List集合一样,Set集合也是不安全类,在并发情况修改下会报:java.util.ConcurrentModificationException(并发修改异常);
/** * Adds the specified element to this set if it is not already present. * More formally, adds the specified element <tt>e</tt> to this set if * this set contains no element <tt>e2</tt> such that * <tt>(e==null ? e2==null : e.equals(e2))</tt>. * If this set already contains the element, the call leaves the set * unchanged and returns <tt>false</tt>. * * @param e element to be added to this set * @return <tt>true</tt> if this set did not already contain the specified * element */ public boolean add(E e) { return map.put(e, PRESENT)==null; }
查看HashSet的源码可知,Ha