HashSet与LinkedHashSet分析

在前面已经介绍过HashMap和LinkedHashMap,在此基础上再来看HashSet和LinkedHashSet的源码会非常简单

HashSet,其内部维护了一个HashMap和一个Object类型的对象,来保存每个元素对应的value值,HashSet其实就是HashMap当中key的集合,value使用的都是同一个object对象。

对应的add方法和remove方法,分别调用的都是HashMap的add和remove方法,因为HashMap当中key不能重复,所以HashSet当中的元素是不能重复的,

LinkedHashMap继承了HashMap, 基本上LinkedHashSet的方法调用的都是HashSet当中的方法,不同的是,LinkedHashSet当中使用的map对象不是hashMap,而是LinkedHashMap

所以关于HashSet和LinkedHashSet这里需要分析的内容不多。

猜你喜欢

转载自blog.csdn.net/summerzbh123/article/details/80963080
今日推荐