import java.util.Set;
import java.util.HashSet; public class SetTest {
public static void main(String[] args) {
/*
*对于用户自己定义类型的数据放在容器(Set)中
*务必重写equals和hashCode方法
*要不然stu1和stu2放在容器中,和觉得是两个不同的元素
**/ //set中存放的元素是无序的
//set中存储的元素是不能够反复的(依据equals方法和hashCode方法推断)
Set set = new HashSet();
Student stu1 = new Student(1, "aaa");
Student stu2 = new Student(1, "aaa");
Student stu3 = new Student(2, "ccc");
Student stu4 = new Student(8, "fff"); set.add(stu1);
set.add(stu2);
set.add(stu3);
set.add(stu4); System.out.println(set);
}
} class Student {
private int id;
private String name; public Student(int id, String name) {
this.id = id;
this.name = name;
} @Override
public String toString() {
return this.id + " " + this.name;
} @Override
public boolean equals(Object obj) {
Student stu = (Student) obj; return this.id == stu.id && this.name.equals(stu.name);
} @Override
public int hashCode() {
return this.id*this.name.hashCode();
}
}
输出结果:
[8 fff, 1 aaa, 2 ccc]

假设不重写hashCode和equals方法

import java.util.Set;
import java.util.HashSet; public class SetTest {
public static void main(String[] args) {
/*
*对于用户自己定义类型的数据放在容器(Set)中
*务必重写equals和hashCode方法
*要不然stu1和stu2放在容器中,和觉得是两个不同的元素
**/ //set中存放的元素是无序的
//set中存储的元素是不能够反复的(依据equals方法和hashCode方法推断)
Set set = new HashSet();
Student stu1 = new Student(1, "aaa");
Student stu2 = new Student(1, "aaa");
Student stu3 = new Student(2, "ccc");
Student stu4 = new Student(8, "fff"); set.add(stu1);
set.add(stu2);
set.add(stu3);
set.add(stu4); System.out.println(set);
}
} class Student {
private int id;
private String name; public Student(int id, String name) {
this.id = id;
this.name = name;
} @Override
public String toString() {
return this.id + " " + this.name;
}
}
输出结果:
[1 aaa, 1 aaa, 8 fff, 2 ccc]

HashSet中存方用户自己定义数据类型数据,重写equals方法和hashCode方法的更多相关文章

  1. java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样

    一.在Object类中的定义为:public native int hashCode();是一个本地方法,返回的对象的地址值.但是,同样的思路,在String等封装类中对此方法进行了重写.方法调用得到 ...

  2. Java基础系列-equals方法和hashCode方法

    原创文章,转载请标注出处:<Java基础系列-equals方法和hashCode方法> 概述         equals方法和hashCode方法都是有Object类定义的. publi ...

  3. 详解equals()方法和hashCode()方法

    前言 Java的基类Object提供了一些方法,其中equals()方法用于判断两个对象是否相等,hashCode()方法用于计算对象的哈希码.equals()和hashCode()都不是final方 ...

  4. Java 如何重写对象的 equals 方法和 hashCode 方法

    前言:Java 对象如果要比较是否相等,则需要重写 equals 方法,同时重写 hashCode 方法,而且 hashCode 方法里面使用质数 31.接下来看看各种为什么. 一.需求: 对比两个对 ...

  5. 关于Object类的equals方法和hashCode方法

    关于Object类的equals的特点,对于非空引用: 1.自反性:x.equals(x) return true : 2.对称性:x.equals(y)为true,那么y.equals(x)也为tr ...

  6. 详解 equals() 方法和 hashCode() 方法

    创建实体类时,最好重写超类(Object)的hashCode()和equals()方法 equals()方法: 通过该实现可以看出,Object类的实现采用了区分度最高的算法,即只要两个对象不是同一个 ...

  7. equals()方法和hashCode()方法详解

    equals()方法和hashCode()方法详解 1. Object类中equals()方法源代码如下所示: /** * Object类中的equals()方法 */ public boolean ...

  8. Java equals()方法和hashCode()方法

    equals()方法 如果满足了以下任何一个条件,就不需要覆盖equals()方法: 1 类的每个实例本质上都是唯一的. 2 不关心类是否提供了“逻辑相等”的测试功能. 3 父类已经覆盖了equals ...

  9. HashSet中的元素必须重写equals方法和hashCode方法

    http://jingyan.baidu.com/article/d5a880eb8fb61d13f147cc99.html 1.为什么必须重写这两个方法. 2.什么事hashSet去重,符合什么样的 ...

随机推荐

  1. python随机数的产生

    导入 random模块  >>> import random 1.  random.random random.random()用于生成一个0到1的随机浮点数: 0 <= n ...

  2. Octave 里的 fminunc

    ptions = optimset('GradObj', 'on', 'MaxIter', '100'); initialTheta = zeros(2,1); [optTheta, function ...

  3. POJ 1690 (Your)((Term)((Project)))

    (Your)((Term)((Project))) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2912   Accept ...

  4. mq类----2

    手动应答方式 使用get my_consumer.php 消费者  生产者和上一篇 一样 <?php /** * Created by PhpStorm. * User: brady * Dat ...

  5. BZOJ 2806 [Ctsc2012]Cheat ——后缀自动机 单调队列优化DP

    先建出广义后缀自动机. 然后跑出文章中每一个位置的最大匹配距离. 然后定义$f[i]$表示匹配到以$i$结尾的串时,最长的匹配距离. 显然可以二分$L$的取值. 然后容易得到$DP$方程 $f[i]= ...

  6. html body width height 100%使用

    首先我们来看一个实际的问题,让body中的一个div占全屏,(问题来源:http://stackoverflow.com/questions/1575141/make-div-100-height-o ...

  7. 微信小程序红包开发思路 微信红包小程序开发思路讲解

    之前公司开发小程序红包,将自己在开发的过程中遇到的一些坑分享到了博客里.不少人看了以后,还是不明白怎么开发.也加了我微信咨询.所以今天,我就特意再写一篇文章,这次就不谈我开发中遇到的坑了.就主要给大家 ...

  8. 外星人(bzoj 2749)

    Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Output 3 HINT Test< ...

  9. 【BZOJ3529】数表(莫比乌斯反演,BIT,自然溢出)

    题意: 思路: #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

  10. 五、 java中数组

    定义数组的两种方式 class myarray1 { public static void main(String[] args) { //1.如何定义一个数组 //1.1数组的声明 String[] ...