类关系图
功能介绍
包路径:java.util
功能描述:Set 继承自Collection接口,只是行为上不同,Set 不保存重复的元素**。Set集合的实现类可说是基于Map集合去写的。通过内部封装Map集合来实现的比如HashSet内部封装了HashMap
Set 接口存储一组唯一,无序的对象。
特点:插入无序,不可指定位置访问。
源码解析
Set
package java.util;
/**
*
* @param <E> the type of elements maintained by this set
*
* @author Josh Bloch
* @author Neal Gafter
* @see Collection
* @see List
* @see SortedSet
* @see HashSet
* @see TreeSet
* @see AbstractSet
* @see Collections#singleton(java.lang.Object)
* @see Collections#EMPTY_SET
* @since 1.2
*/
public interface Set<E> extends Collection<E> {
// Query Operations
/**
* 获取集合元素数量
* @return the number of elements in this set (its cardinality)
*/
int size();
/**
* 判断集合是否为空
* @return <tt>true</tt> if this set contains no elements
*/
boolean isEmpty();
/**
* 判断集合内是否存在目标元素
* @param o element whose presence in this set is to be tested
* @return <tt>true</tt> if this set contains the specified element
*/
boolean contains(Object o);
/**
* 返回迭代器
* @return an iterator over the elements in this set
*/
Iterator<E> iterator();
/**
* 将集合转为数组返回
* @return an array containing all the elements in this set
*/
Object[] toArray();
/**
* 将集合转为目标类型的数组返回
* @param a the array into which the elements of this set are to be
* stored, if it is big enough; otherwise, a new array of the same
* runtime type is allocated for this purpose.
* @return an array containing all the elements in this set
*/
<T> T[] toArray(T[] a);
// Modification Operations
/**
* 添加元素
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
boolean add(E e);
/**
* 从集合中移除元素
* @param o object to be removed from this set, if present
* @return <tt>true</tt> if this set contained the specified element
*/
boolean remove(Object o);
// Bulk Operations
/**
* 判断是否存在传入集合的所有元素(交集)
* @param c collection to be checked for containment in this set
* @return <tt>true</tt> if this set contains all of the elements of the
* specified collection
*/
boolean containsAll(Collection<?> c);
/**
* 添加传入集合的所有元素
* @param c collection containing elements to be added to this set
* @return <tt>true</tt> if this set changed as a result of the call
* @see #add(Object)
*/
boolean addAll(Collection<? extends E> c);
/**
* 仅保留传入集合里存在的元素(求交集)
* @param c collection containing elements to be retained in this set
* @return <tt>true</tt> if this set changed as a result of the call
* @see #remove(Object)
*/
boolean retainAll(Collection<?> c);
/**
* 仅移除传入集合的所有元素
* @param c collection containing elements to be removed from this set
* @return <tt>true</tt> if this set changed as a result of the call
* @see #remove(Object)
* @see #contains(Object)
*/
boolean removeAll(Collection<?> c);
/**
* 清空集合
*/
void clear();
// Comparison and hashing
/**
* 判断集合是否相等(集合内所有元素相等则为相等)
* @param o object to be compared for equality with this set
* @return <tt>true</tt> if the specified object is equal to this set
*/
boolean equals(Object o);
/**
*
* @return the hash code value for this set
* @see Object#equals(Object)
* @see Set#equals(Object)
*/
int hashCode();
/**
* 返回切割器
* @return a {@code Spliterator} over the elements in this set
* @since 1.8
*/
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT);
}
}
AbstractSet
AbstractSet是一个抽象类,实现了Set接口,同时继承了AbstractCollection抽象类
package java.util;
/**
*
* @author Josh Bloch
* @author Neal Gafter
* @see Collection
* @see AbstractCollection
* @see Set
* @since 1.2
*/
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
/**
* Sole constructor. (For invocation by subclass constructors, typically
* implicit.)
*/
protected AbstractSet() {
}
// Comparison and hashing
/**
* 比较两个集合是否相等
* @param o object to be compared for equality with this set
* @return <tt>true</tt> if the specified object is equal to this set
*/
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Set))
return false;
Collection<?> c = (Collection<?>) o;
if (c.size() != size())
return false;
try {
// 是否全部存在
return containsAll(c);
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
}
/**
*
* @return the hash code value for this set
* @see Object#equals(Object)
* @see Set#equals(Object)
*/
public int hashCode() {
int h = 0;
Iterator<E> i = iterator();
while (i.hasNext()) {
E obj = i.next();
if (obj != null)
h += obj.hashCode();
}
return h;
}
/**
* 从当前集合中移除传入集合包含的所有元素
* @param c collection containing elements to be removed from this set
* @return <tt>true</tt> if this set changed as a result of the call
* @see #remove(Object)
* @see #contains(Object)
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
// 如果当前集合元素比传入集合元素多
if (size() > c.size()) {
// 迭代传入集合并从当前集合移除
for (Iterator<?> i = c.iterator(); i.hasNext(); )
modified |= remove(i.next());
} else {
// 迭代当前集合
for (Iterator<?> i = iterator(); i.hasNext(); ) {
if (c.contains(i.next())) {
i.remove();
modified = true;
}
}
}
return modified;
}
}
简单使用
public class SetCode {
public static void main(String[] args) {
commonExample();
}
public static void commonExample(){
Set<Integer> mySet = new HashSet<>();
for(int i=0;i<20;i++){
mySet.add(i);
}
System.out.println("mySet = "+mySet);
System.out.println("mySet.size() = "+mySet.size());
Set<Integer> subSet = new HashSet<>();
for(int i=0;i<10;i++){
subSet.add(i);
}
System.out.println("subSet = "+subSet);
System.out.println("mySet.containsAll(subSet) = "+mySet.containsAll(subSet));
mySet.removeAll(subSet);
System.out.println("mySet = "+mySet);
}
}
输出:
mySet = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
mySet.size() = 20
subSet = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
mySet.containsAll(subSet) = true
mySet = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
总结
1、Set 继承自Collection接口,只是行为上不同,Set 不保存重复的元素。Set集合的实现类可说是基于Map集合去写的。通过内部封装Map集合来实现的比如HashSet内部封装了HashMap
2、Set 接口存储一组唯一,无序的对象。
3、Set插入无序,不可指定位置访问。
思考
Set和List的区别
- Set 接口实例存储的是无序的,不重复的数据。List 接口实例存储的是有序的,可以重复的元素。
- Set 接口实例存储的是无序的,不重复的数据。List 接口实例存储的是有序的,可以重复的元素。
- Set检索效率低下,删除和插入效率高,插入和删除不会引起元素位置改变 <实现类有HashSet,TreeSet>。
- Set检索效率低下,删除和插入效率高,插入和删除不会引起元素位置改变 <实现类有HashSet,TreeSet>。
- List和数组类似,可以动态增长,根据实际存储的数据的长度自动增长List的长度。查找元素效率高,插入删除效率低,因为会引起其他元素位置改变 <实现类有ArrayList,LinkedList,Vector> 。
- List和数组类似,可以动态增长,根据实际存储的数据的长度自动增长List的长度。查找元素效率高,插入删除效率低,因为会引起其他元素位置改变 <实现类有ArrayList,LinkedList,Vector> 。