Jdk1.6 Collections Framework源码解析(7)-HashSet、LinkedHashSet

本文对比分析了HashSet和LinkedHashSet两种集合类的实现原理。HashSet内部通过HashMap实现元素的存储,保证元素唯一性的同时不考虑元素顺序。LinkedHashSet继承自HashSet,通过指定HashMap的实现为LinkedHashMap来保持元素插入顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇总结一下两个常用的集合类HashSet和LinkedHashSet。
它们都实现了相同接口java.util.Set。Set表示一种元素无序且不可重复的集合;之前总结过的java.util.List表示一种元素可重复且有序的集合。它们都扩展自java.util.Collection。

public interface Set<E> extends Collection<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean retainAll(Collection<?> c);
boolean removeAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();

可以看到Set的接口描述,和List比,只少了一些与下标有关的特性。

先来看一下HashSet的实现方式。

public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{

HashSet是[url=http://brokendreams.iteye.com/blog/1940038][color=orange]可克隆[/color][/url]、可序列化的;实现了java.util.Set;继承了java.util.AbstractSet,AbstractSet包含一些骨架方法,很容易看懂。继续往下看。

private transient HashMap<E,Object> map;

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

看到这里就基本上明白是怎么实现的了吧!原来HashSet内部是一个HashMap,难怪HashSet用起来很像一个HashMap,只是没有value。所以PRESENT这个对象就用来填充value值了。有了前面[url=http://brokendreams.iteye.com/blog/1926114]HashMap的分析[/url],后面的基本上不用看了。
不过有一个小地方要注意一下。

/**
* Constructs a new, empty linked hash set. (This package private
* constructor is only used by LinkedHashSet.) The backing
* HashMap instance is a LinkedHashMap with the specified initial
* capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @param dummy ignored (distinguishes this
* constructor from other int, float constructor.)
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}

这个构造方法和其他的不一样。里面的Map实现是LinkedHashMap。从注释上可以看到,这个方法只是由LinkedHashSet使用,方法的第三个参数也只是用来与其他构造方法进行区分,代码中并没有实际用到。

接下来看一下java.util.LinkedHashSet的实现吧。

public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {

private static final long serialVersionUID = -2851667679971038690L;

/**
* Constructs a new, empty linked hash set with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity of the linked hash set
* @param loadFactor the load factor of the linked hash set
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}

/**
* Constructs a new, empty linked hash set with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity of the LinkedHashSet
* @throws IllegalArgumentException if the initial capacity is less
* than zero
*/
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
}

/**
* Constructs a new, empty linked hash set with the default initial
* capacity (16) and load factor (0.75).
*/
public LinkedHashSet() {
super(16, .75f, true);
}

/**
* Constructs a new linked hash set with the same elements as the
* specified collection. The linked hash set is created with an initial
* capacity sufficient to hold the elements in the specified collection
* and the default load factor (0.75).
*
* @param c the collection whose elements are to be placed into
* this set
* @throws NullPointerException if the specified collection is null
*/
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true);
addAll(c);
}
}

以上便是LinkedHashSet的全部代码了,基本上没什么可说的了,参考下[url=http://brokendreams.iteye.com/blog/1927490]LinkedHashMap的总结[/url]吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值