一点一点看源码(1)未完

3 篇文章 0 订阅

一:Collection体系概览

容器的存储,数据的获取,数据添加,数据遍历,数据搜索,其他(判空,替换,排序,溢出,并发,同步,拆分,汇聚)

  @see     Set
  @see     List
  @see     Map
  @see     SortedSet
  @see     SortedMap
  @see     HashSet
  @see     TreeSet
  @see     ArrayList
  @see     LinkedList
  @see     Vector
  @see     Collections
  @see     Arrays
  @see     AbstractCollection

集合作为容器,其体系中的所有集合都包括了:

  大小(size),判空(isEmpty),添加元素(add),删除元素(remove),是否包含(contains)

  转换数组(toArray),清空(clear),遍历与迭代(forEach(父接口中),iterator)

  是否相同(equals),哈希(hashCode),求交集(retainAll)

  除此之外,提供了java8的分离接口,聚合接口,为了大容量集合的多线程操作

二:map

“A map cannot contain duplicate keys; each key can map to at most one value.”
一个map不能包含重复的key,每个key最多映射一个value。
“This interface takes the place of the <tt>Dictionary</tt> class”Dictionary类是用来存储键值对的类与map类似。

"The <tt>Map</tt> interface provides three <i>collection views</i>, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings.  The <i>order</i> of a map is defined as the order in which the iterators on the map's collection views return their elements.  Some map implementations, like the <tt>TreeMap</tt> class, make specific guarantees as to their order; others, like the <tt>HashMap</tt> class, do not."Map接口提供三个集合视图,1.key的集合 2.value的集合 3.key-value的集合。map内元素的顺序取决于Iterator的具体实现,获取集合视图其实是获取一个迭代器,实现对遍历元素细节的隐藏。TreeMap类能保证遍历元素的顺序,而HashMap就无法保证遍历元素的顺序。map是根据hashCode和equals方法决定存放的位置的.

 

三:set

源码中写到:“A collection that contains no duplicate elements”  不能有重复元素

           “and at most one null element.”并且最多只有一个空元素。

  @see Collection  @see List  @see SortedSet  @see HashSet  @see TreeSet  @see AbstractSet

1:Set集合与List一样,都是继承自Collection接口,常用的实现类有HashSet和TreeSet。值得注意的是,HashSet是通过HashMap来实现的而TreeSet是通过TreeMap来实现的,所以HashSet和TreeSet都没有自己的数据结构,具体可以归纳如下:
    Set集合中的元素不能重复,即元素唯一
    HashSet按元素的哈希值存储,所以是无序的,并且最多允许一个null对象
    TreeSet按元素的大小存储,所以是有序的,并且不允许null对象
    Set集合没有get方法,所以只能通过迭代器(Iterator)来遍历元素,不能随机访问
2:HashSet:

注解的大概摘要:

“This class implements the <tt>Set</tt> interface, backed by a hash table (actually a <tt>HashMap</tt> instance).”

HashSet是由HashMap实现的,

“Thus, it's very important not to set the initial capacity too high (or the load factor too low)” 初始化数量不要太高或低,

“Note that this implementation is not synchronized”不是线程安全的,如果多个线程操作,

“This is typically accomplished by synchronizing on some object that naturally encapsulates the set”
通过自然地封装该集合的某个对象上进行同步来完成,如果实在不行就:“Set s = Collections.synchronizedSet(new HashSet(...))”  没看明白这个操作,
public class HashSet<E>  extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable{
    static final long serialVersionUID = -5024744406713321676L;
    private transient HashMap<E,Object> map;//用transient关键字标记的成员变量不参与序列化过程
    private static final Object PRESENT = new Object();

未完  待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值