集合框架-HashSet存储字符串、自定义对象并遍历
2017-04-26 11:11
495 查看
package cn.itcast_02; import java.util.HashSet; /* * 需求:存储自定义对象,并保证元素的唯一性 * 要求:如果两个对象的成员变量值都相同,则为同一个元素。 * * 目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。 * 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类的。 * 这个时候,他们的哈希值是不会一样的,根本就不会继续判断,执行了添加操作。 * * */ public class HashSetDemo2 { public static void main(String[] args) { // 创建集合对象 HashSet<Student> hs = new HashSet<Student>(); // 创建学生对象 Student s1 = new Student("林青霞", 27); Student s2 = new Student("柳岩", 22); Student s3 = new Student("王祖贤", 30); Student s4 = new Student("林青霞", 27); Student s5 = new Student("林青霞", 20); Student s6 = new Student("范冰冰", 22); // 添加元素 hs.add(s1); hs.add(s2); hs.add(s3); hs.add(s4); hs.add(s5); hs.add(s6); // 遍历集合 for (Student s : hs) { System.out.println(s.getName() + "---" + s.getAge()); } } }
package cn.itcast_02;
/**
* @author Administrator
* 重写hashCode()和equals()方法
*/
public class Student {
private String name;
private int age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//以下代码都不用自己写,可以自动生成
@Override
public int hashCode() {
return 0;
// 因为成员变量值影响了哈希值,所以我们把成员变量值相加即可
// return this.name.hashCode() + this.age;
// 看下面
// s1:name.hashCode()=40,age=30
// s2:name.hashCode()=20,age=50
// 尽可能的区分,我们可以把它们乘以一些整数
return this.name.hashCode() + this.age * 15;
}
@Override
public boolean equals(Object obj) {
System.out.println(this + "---" + obj);
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age;
}
HashSet存储自定义对象并遍历:
package cn.itcast_03;
import java.util.HashSet;
/*
* HashSet集合存储自定义对象并遍历。如果对象的成员变量值相同即为同一个对象
*
* 注意了:
* 你使用的是HashSet集合,这个集合的底层是哈希表结构。
* 而哈希表结构底层依赖:hashCode()和equals()方法。
* 如果你认为对象的成员变量值相同即为同一个对象的话,你就应该重写这两个方法。
* 如何重写呢?不同担心,自动生成即可。
*/
public class DogDemo {
public static void main(String[] args) {
// 创建集合对象
HashSet<Dog> hs = new HashSet<Dog>();
// 创建狗对象
Dog d1 = new Dog("秦桧", 25, "红色", '男');
Dog d2 = new Dog("高俅", 22, "黑色", '女');
Dog d3 = new Dog("秦桧", 25, "红色", '男');
Dog d4 = new Dog("秦桧", 20, "红色", '女');
Dog d5 = new Dog("魏忠贤", 28, "白色", '男');
Dog d6 = new Dog("李莲英", 23, "黄色", '女');
Dog d7 = new Dog("李莲英", 23, "黄色", '女');
Dog d8 = new Dog("李莲英", 23, "黄色", '男');
// 添加元素
hs.add(d1);
hs.add(d2);
hs.add(d3);
hs.add(d4);
hs.add(d5);
hs.add(d6);
hs.add(d7);
hs.add(d8);
// 遍历
for (Dog d : hs) {
System.out.println(d.getName() + "---" + d.getAge() + "---"
+ d.getColor() + "---" + d.getSex());
}
}
}
package cn.itcast_03; public class Dog { private String name; private int age; private String color; private char sex; public Dog() { super(); } public Dog(String name, int age, String color, char sex) { super(); this.name = name; this.age = age; this.color = color; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((color == null) ? 0 : color.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + sex; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Dog other = (Dog) obj; if (age != other.age) return false; if (color == null) { if (other.color != null) return false; } else if (!color.equals(other.color)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex != other.sex) return false; return true; } }
相关文章推荐
- 集合框架_HashSet存储自定义对象并遍历
- 集合框架_HashSet存储自定义对象并遍历练习
- 集合框架-ArrayList存储字符串、自定义对象并遍历泛型版
- Java基础知识强化之集合框架笔记24:ArrayList存储自定义对象并遍历
- Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序)
- 集合框架_TreeSet存储自定义对象并遍历练习2)
- 集合框架(List存储自定义对象并遍历)
- 集合框架_ArrayList存储自定义对象并遍历
- List集合存储字符串和自定义对象并遍历(for循环和迭代)
- 15-常用对象API(集合框架-ArrayList集合存储自定对象) 1 2 3 16-常用对象API(集合框架-HashSet集合) 19-常用对象API(集合框架-HashSet存储自定义
- 集合框架_Collection存储自定义对象并遍历案例
- 集合框架(ArrayList存储自定义对象并遍历泛型版)
- 集合框架_ArrayList存储自定义对象并遍历泛型版
- 集合框架_HashSet存储字符串并遍历
- 集合框架_Collection存储自定义对象并遍历案例
- Java基础知识强化之集合框架笔记41:Set集合之HashSet存储自定义对象并遍历练习
- Java基础知识强化之集合框架笔记46:Set集合之TreeSet存储自定义对象并遍历练习2(自然排序)
- Java基础知识强化之集合框架笔记40:Set集合之HashSet存储自定义对象并遍历
- 17-集合框架-19-常用对象API(集合框架-HashSet存储自定义对象)
- 集合框架(Collection存储自定义对象并遍历案例)