您的位置:首页 > 编程语言 > Java开发

Java 集合框架分析:Set(1)

2016-10-04 13:59 337 查看
相关文章:

Java 集合框架分析:Set

http://blog.csdn.net/youyou1543724847/article/details/52733723

Java 集合框架分析:LinkedList

http://blog.csdn.net/youyou1543724847/article/details/52734935

Java 集合框架分析:DelayQueue

http://blog.csdn.net/youyou1543724847/article/details/52176504

Java 集合框架分析:ArrayBlockingQueue

http://blog.csdn.net/youyou1543724847/article/details/52174308

Java 集合框架分析:ArrayDeque

http://blog.csdn.net/youyou1543724847/article/details/52170026

Java 集合框架分析:PriorityBlockingQueue

http://blog.csdn.net/youyou1543724847/article/details/52166985

Java 集合框架分析:JAVA Queue源码分析

http://blog.csdn.net/youyou1543724847/article/details/52164895

Java 集合框架分析:关于Set,Map集合中元素判等的方式

http://blog.csdn.net/youyou1543724847/article/details/52733766

Java 集合框架分析:ConcurrentModificationException

http://blog.csdn.net/youyou1543724847/article/details/52733780

Java 集合框架分析:线程安全的集合

http://blog.csdn.net/youyou1543724847/article/details/52734876

Java 集合框架分析:JAVA集合中的一些边边角角的知识

http://blog.csdn.net/youyou1543724847/article/details/52734918

常用的两种set:TreeSet,HashSet

实现:都是基于相应的map接口来的,如TreeSet底层用的TreeMap,HashSet底层用的HashMap。映射到map,key为set中你要放置的量,value为一个全局的object对象(节约空间)

TreeSet

看一下源码结构

public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable
{
/**
* The backing map.
*/
private transient NavigableMap<E,Object> m;

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

/**
* Constructs a set backed by the specified navigable map.
*/
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}

/**
* Constructs a new, empty tree set, sorted according to the
* natural ordering of its elements.  All elements inserted into
* the set must implement the {@link Comparable} interface.
* Furthermore, all such elements must be <i>mutually
* comparable</i>: {@code e1.compareTo(e2)} must not throw a
* {@code ClassCastException} for any elements {@code e1} and
* {@code e2} in the set.  If the user attempts to add an element
* to the set that violates this constraint (for example, the user
* attempts to add a string element to a set whose elements are
* integers), the {@code add} call will throw a
* {@code ClassCastException}.
*/
public TreeSet() {
this(new TreeMap<E,Object>());
}
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}


特性:

基于树的实现,能保证元素的排列顺序,且插入、查找、删除的时间为log(n)



HashSet

结构和TreeSet大同小异

特性:

基于hash表实现,不保持元素的顺序,(This class implements the Set interface, backed by a hash table (actually a HashMap instance).

提供常量时间的性能,对于add, remove, contains and size(如果hash函数将元素散开在不同的桶上的话) Iterating over this set requires time proportional to the sum of the HashSet instance’s size (the number of elements) plus the “capacity” of the backing HashMap instance (the number of buckets). Thus, it’s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important. 在HashSet上的迭代的时间和 hashset实例的大小+容器的capacity大小之和 成正比
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java JAVA集合