一、Set接口简介
- Set接口和List接口一样,同样继承自Collection接口
- 它与Collection接口中的方法基本一致,没有对Collection接口进行功能上的扩充,只是比Collection接口更加严格了
- 与list接口不同的是,Set接口中的元素无序,并且都会以某种规则保证存入的元素不出现重复
- Set接口主要有两个实现类,分别是HashSet 和 TreeSet。
HashSet是根据对象的哈希值来确定元素在集合里的存储位置,因此具有良好的存取和查找性能
TreeSet则是以二叉树的方式来存取
1、HashSet
(1)存入相同String
package cn.itcast.chapter06.example07;
import java.util.*;
/**
* HashSet集合的用法
*/
public class Example07 {
public static void main(String[] args) {
HashSet set = new HashSet(); // 创建HashSet集合
set.add("Jack"); // 向该Set集合中添加字符串
set.add("Eve");
set.add("Rose");
set.add("Rose"); // 向该Set集合中添加重复元素
Iterator it = set.iterator(); // 获取Iterator对象
while (it.hasNext()) { // 通过while循环,判断集合中是否有元素
Object obj = it.next(); // 如果有元素,就通过迭代器的next()方法获取元素
System.out.println(obj);
}
}
}
输出
Eve
Rose
Jack
(2)存入“ 相同 ” 对象
只要new 出一个对象,内存里就会分配一片内存空间,即使成员变量相同,也是不同的对象
package cn.itcast.chapter06.example08;
import java.util.HashSet;
class Student {
String id;
String name;
public Student(String id, String name) { // 创建构造方法
this.id = id;
this.name = name;
}
public String toString() { // 重写toString()方法
return id + ":" + name;
}
}
/**
* HashSet集合的用法
*/
public class Example08 {
public static void main(String[] args) {
HashSet hs = new HashSet(); // 创建HashSet集合
Student stu1 = new Student("1", "Jack"); // 创建Student对象
Student stu2 = new Student("2", "Rose");
Student stu3 = new Student("2", "Rose"); //内存空间不同
hs.add(stu1);
hs.add(stu2);
hs.add(stu3);
System.out.println(hs);
}
}
输出
[2:Rose, 1:Jack, 2:Rose]
(3) void add(Object obj)详解
在调用add() 时,会首先调用hashCode()方法,获得当前待存入对象的哈希值,然后根据哈希值算出一个存储位置。
如果该位置上没有元素,直接存入;
有元素,则调用equals()方法比较两个元素:
返回false则存入,
返回true则舍弃
//3个对象的哈希值不同
package cn.itcast.chapter06.example09;
import java.util.*;
class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
// 重写toString()方法
public String toString() {
return id + ":" + name;
}
}
public class Example09 {
public static void main(String[] args) {
HashSet hs = new HashSet(); // 创建HashSet对象
Student stu1 = new Student("1", "Jack"); // 创建Student对象
Student stu2 = new Student("2", "Rose");
Student stu3 = new Student("2", "Rose");
System.out.println(stu1.hashCode());
System.out.println(stu2.hashCode());
System.out.println(stu3.hashCode());
hs.add(stu1); // 向集合存入对象
hs.add(stu2);
hs.add(stu3);
System.out.println(hs); // 打印集合中的元素
}
}
输出
1163157884
1956725890
356573597
[2:Rose, 1:Jack, 2:Rose]
// 重写hashCode(), equals方法
package cn.itcast.chapter06.example09;
import java.util.HashSet;
class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
// 重写toString()方法
public String toString() {
return id + ":" + name;
}
// 重写hashCode方法
public int hashCode() {
return id.hashCode(); // 返回id属性的哈希值
}
// 重写equals方法
public boolean equals(Object obj) {
if (this == obj) { // 判断是否是同一个对象
return true; // 如果是,直接返回true
}
if (!(obj instanceof Student)) { // 判断对象是为Student类型
return false; // 如果对象不是Student类型,返回false
}
Student stu = (Student) obj; // 将对象强转为Student类型
boolean b = this.id.equals(stu.id); // 判断id值是否相同
return b; // 返回判断结果
}
}
public class Example09 {
public static void main(String[] args) {
HashSet hs = new HashSet(); // 创建HashSet对象
Student stu1 = new Student("1", "Jack"); // 创建Student对象
Student stu2 = new Student("2", "Rose");
Student stu3 = new Student("2", "Rose");
System.out.println(stu1.hashCode());
System.out.println(stu2.hashCode());
System.out.println(stu3.hashCode());
hs.add(stu1); // 向集合存入对象
hs.add(stu2);
hs.add(stu3);
System.out.println(hs); // 打印集合中的元素
}
}
输出
49
50
50
[1:Jack, 2:Rose]
二、