码迷,mamicode.com
首页 > 其他好文 > 详细

HashSet存储元素保证唯一性的代码及图解

时间:2018-03-28 01:30:27      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:技术分享   ima   hashset   图片   自动   color   学生   import   tcl   

需求:存储自定义对象,并保证元素的唯一性

要求:如果两个对象的成员变量值都相同,则为同一个元素。

技术分享图片

示例代码如下:

  1 package cn.itcast_02;
  2 
  3 /**
  4  * @author Administrator
  5  * 
  6  */
  7 public class Student {
  8     private String name;
  9     private int age;
 10 
 11     public Student() {
 12         super();
 13     }
 14 
 15     public Student(String name, int age) {
 16         super();
 17         this.name = name;
 18         this.age = age;
 19     }
 20 
 21     public String getName() {
 22         return name;
 23     }
 24 
 25     public void setName(String name) {
 26         this.name = name;
 27     }
 28 
 29     public int getAge() {
 30         return age;
 31     }
 32 
 33     public void setAge(int age) {
 34         this.age = age;
 35     }
 36 
 37     // 自动生成(快捷方式生成)
 38     @Override
 39     public int hashCode() {
 40         final int prime = 31;
 41         int result = 1;
 42         result = prime * result + age;
 43         result = prime * result + ((name == null) ? 0 : name.hashCode());
 44         return result;
 45     }
 46 
 47     @Override
 48     public boolean equals(Object obj) {
 49         if (this == obj)
 50             return true;
 51         if (obj == null)
 52             return false;
 53         if (getClass() != obj.getClass())
 54             return false;
 55         Student other = (Student) obj;
 56         if (age != other.age)
 57             return false;
 58         if (name == null) {
 59             if (other.name != null)
 60                 return false;
 61         } else if (!name.equals(other.name))
 62             return false;
 63         return true;
 64     }
 65 
 66     /*
 67     @Override
 68     public int hashCode() {
 69         // return 0;
 70         // 因为成员变量的值影响了哈希值,所以我们把成员变量值相加即可
 71         // return this.name.hashCode() + this.age;
 72         // 看下面
 73         // 如果s1:
 74                 name.hashCode() = 40, age = 30
 75          // 如果s2:
 76                 name.hashCode() = 20, age = 50
 77         // 为了尽可能的区分他们,我们可以把它们乘以一些整数,例如如下所示:
 78         return this.name.hashCode() + this.age * 15;
 79     }
 80     
 81     @Override
 82     public boolean equals(Object obj) {
 83         // System.out.println(this + "---" + obj);
 84         if (this == obj) {
 85             return true;
 86         }
 87     
 88         if (!(obj instanceof Student)) {
 89             return false;
 90         }
 91     
 92         Student s = (Student) obj;
 93             return this.name.equals(s.name) && this.age == s.age;
 94     }
 95     
 96     @Override
 97     public String toString() {
 98         return "Student [name=" + name + ", age=" + age + "]";
 99     }
100     */
101 
102 }
 1 package cn.itcast_02;
 2 
 3 import java.util.HashSet;
 4 
 5 /*
 6  * 需求:存储自定义对象,并保证元素的唯一性
 7  * 要求:如果两个对象的成员变量值都相同,则为同一个元素。
 8  * 
 9  *         目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。
10  *         而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类的。
11  *         这个时候,一般来说,他们的哈希值是不会一样的,根本就不会继续判断了,就执行了添加操作。
12  */
13 public class HashSetDemo2 {
14     public static void main(String[] args) {
15         // 创建集合对象
16         HashSet<Student> hs = new HashSet<Student>();
17 
18         // 创建学生对象
19         Student s1 = new Student("林青霞", 27);
20         Student s2 = new Student("柳岩", 22);
21         Student s3 = new Student("王祖贤", 30);
22         Student s4 = new Student("林青霞", 27);
23         Student s5 = new Student("林青霞", 20);
24         Student s6 = new Student("范冰冰", 22);
25 
26         // 添加元素
27         hs.add(s1);
28         hs.add(s2);
29         hs.add(s3);
30         hs.add(s4);
31         hs.add(s5);
32         hs.add(s6);
33 
34         // 遍历集合
35         for (Student s : hs) {
36             System.out.println(s.getName() + "---" + s.getAge());
37         }
38     }
39 }

 

HashSet存储元素保证唯一性的代码及图解

标签:技术分享   ima   hashset   图片   自动   color   学生   import   tcl   

原文地址:https://www.cnblogs.com/chenmingjun/p/8661041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!