import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; public class SetTest {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("abc");
set.add("xyz");
set.add("abc"); for (Iterator<String> it = set.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}

查看add方法

/**
* Adds the specified element to this set if it is not already present.
* More formally, adds the specified element <tt>e</tt> to this set if
* this set contains no element <tt>e2</tt> such that
* <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
* unchanged and returns <tt>false</tt>.
*
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

查看map.put方法:

 /**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, key, value, i);
return null;
}

关于set:当向Set集合中添加对象时,集合首先要计算出待添加对象的hashCode值,根据该值来得到一个位置,用于存放当前的对象。如果该位置没有对象存在,那么集合Set就会认为该对象在集合中不存在,直接添加进去。如果该位置已经有一个对象存在,接着将准备添加到集合中的对象与该位置上的对象进行equals方法比较,如果该equals方法返回false,那么集合认为该集合中不存在该对象的,再进行一次散列,将该对象放到散列后计算的地址里。如果equals方法返回true,那么集合认为该对象已经存在了,不会再将对象添加到集合中。

在上面的例子中我们的执行结果为:

true
true
false
abc
xyz

第三个abc插入失败了,原因就是String类型的字符串“abc”的hashcode是相等的而且equals也是相等的(equals方法使一个字符一个字符的进行比较)。


再看下面的例子:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; public class SetTest {
public static void main(String[] args) {
/* Set<String> set = new HashSet<>();
System.out.println(set.add("abc"));
System.out.println(set.add("xyz"));
System.out.println(set.add("abc")); for (Iterator<String> it = set.iterator(); it.hasNext();) {
System.out.println(it.next());
}*/
Set<People> set2 = new HashSet<>();
set2.add(new People("zhangsan"));
set2.add(new People("lisi"));
set2.add(new People("zhangsan"));
for(Iterator<People> it = set2.iterator();it.hasNext();){
System.out.println(it.next().getName());
}
}
}
class People{
String name;
public People(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}

运行结果为:

lisi
zhangsan
zhangsan

同样是zhangsan却能添加两次。

如果想根据name来判断,修改代码如下:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; public class SetTest {
public static void main(String[] args) {
/*
* Set<String> set = new HashSet<>();
* System.out.println(set.add("abc"));
* System.out.println(set.add("xyz"));
* System.out.println(set.add("abc"));
*
* for (Iterator<String> it = set.iterator(); it.hasNext();) {
* System.out.println(it.next()); }
*/
/*
* String a = "abc"; String b = "abc"; System.out.println(a.hashCode());
* System.out.println(b.hashCode());
*/
Set<People> set2 = new HashSet<>();
set2.add(new People("zhangsan"));
set2.add(new People("lisi"));
set2.add(new People("zhangsan"));
for (Iterator<People> it = set2.iterator(); it.hasNext();) {
System.out.println(it.next().getName());
}
}
} class People {
String name; public People(String name) {
this.name = name;
} public String getName() {
return this.name;
} @Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof People) {
People people = (People) obj;
if(this.name.equals(people.getName()));
return true;
}
return false;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}

执行结果为:

lisi
zhangsan

当重写equals方法时,必须要重写hashCode方法。如果一个类的两个对象,使用equals方法比较时返回true,那么这两个对象必须要具有相同的hashCode。

HashSet源码分析1的更多相关文章

  1. HashSet源码分析

    在java集合中有一种集合Set(集),他有两个实现类,分别是HashSet,TreeSet.下面仔细分析HashSet源码. 看了HashSet的源码就会发现HashSet的底层实现是利用HashM ...

  2. 死磕 java集合之HashSet源码分析

    问题 (1)集合(Collection)和集合(Set)有什么区别? (2)HashSet怎么保证添加元素不重复? (3)HashSet是否允许null元素? (4)HashSet是有序的吗? (5) ...

  3. 【Java入门提高篇】Day26 Java容器类详解(八)HashSet源码分析

    前面花了好几篇的篇幅把HashMap里里外外说了个遍,大家可能对于源码分析篇已经讳莫如深了.别慌别慌,这一篇来说说集合框架里最偷懒的一个家伙——HashSet,为什么说它是最偷懒的呢,先留个悬念,看完 ...

  4. HashSet源码分析:JDK源码系列

    1.简介 继续分析源码,上一篇文章把HashMap的分析完毕.本文开始分析HashSet简单的介绍一下. HashSet是一个无重复元素集合,内部使用HashMap实现,所以HashMap的特征耶继承 ...

  5. java.util.HashSet源码分析

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

  6. JAVA的HashSet源码分析

    一.HashSet概述 HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用null元素. 二.HashS ...

  7. Java集合之HashSet源码分析

    概述 HashSet是基于HashMap来实现的, 底层采用HashMap的key来保存数据, 借此实现元素不重复, 因此HashSet的实现比较简单, 基本上的都是直接调用底层HashMap的相关方 ...

  8. HashSet源码分析 jdk1.6

    Set的特点:Set元素无顺序,且元素不可以重复. 1.定义 public class HashSet<E> extends AbstractSet<E> implements ...

  9. HashSet源码分析2

    package com.test1; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public ...

随机推荐

  1. jsp 访问文件夹中的图片,tomcat配置虚拟目录

    1.配置hosts文件 找到C:\Windows\System32\drivers\etc\hosts.txt 文件 添加127.0.0.1  www.image.com  在dos 命令中执行 pi ...

  2. [翻译] API测试最佳实践 - 组织你的测试

    组织你的测试 适用级别:初学者 在最底层,一个测试步骤(Test Step)用来验证一个单独的操作.组合若干测试步骤到测试用例,允许你验证那些被分隔出来的一个一个的功能,这些功能是应用程序所需要的.接 ...

  3. Cygwin, MinGW/MSYS, MinGW-W64/MSYS2

    1. Cygwin http://www.cygwin.com/ Cygwin is a large collection of GNU and Open Source tools which pro ...

  4. AIX 10G HA RAC卸载

    删除 1:crs_stat –t资源都停掉 2:停ha 3: 删除oracle 4:删除crs 5: 删除ha smit hacmp 6: 删除vg exportvg 7;卸载hacmp smitty

  5. Method Dispatch in Protocol Extensions

    We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different d ...

  6. Vue 点击事件怎么传递 this ?

    Part.1 问题 如何使上面的三个按钮单个点击后实现第一个按钮现在的样式呢? Part.2 思路 为当前点击的按钮添加一个 单独的类名,我的做法: .active { background: #3C ...

  7. PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (五) -- 自动 ACK、手动 ACK、NACK

    以 Direct 类型的 交换机和 Queue 的 get 方法为例. producer.php // 连接设置 $conConfig = [ 'host' => '127.0.0.1', 'p ...

  8. MRC转ARC

    转载请注明出处:http://blog.csdn.net/cywn_d/article/details/18222671 1.删除所有retain,release和autorelease. 2.把原来 ...

  9. ProtoBuf - Arena

    1.概述 最近看 Protocal Buffer 的源码,初次见到这个库源自陈硕的 muduo ,便打算看一看,在此做一下记录.官网文档不能访问,只能凭借代码的自己理解,查看的源码版本为 3.6.0. ...

  10. MapReduce实例——查询缺失扑克牌

    问题: 解决: 首先分为两个过程,Map过程将<=10的牌去掉,然后只针对于>10的牌进行分类,Reduce过程,将Map传过来的键值对进行统计,然后计算出少于3张牌的的花色 1.代码 1 ...