package com.bjpowernode.set;
import com.sun.media.sound.SoftTuning;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Set集合
*/
public class Test01 {
public static void main(String[] args) {
//1) 创建Set集合存储String字符串, 通过泛型指定存储元素的类型是String, Set是一个接口需要赋值实现类对象
Set<String > set = new HashSet<>();
//2) 添加元素
set.add("xx");
set.add("oo");
set.add("123456");
set.add("xx");
set.add("oo");
set.add("jj");
//3) 直接打印, 存储顺序与添加顺序可能不一样, 不能存储重复的元素
System.out.println(set); //[xx, oo, jj, 123456]
System.out.println( set.size() ); //4
System.out.println(set.contains("jj")); //true
set.remove("xx");
System.out.println(set); //[oo, jj, 123456]
for (String s : set) {
System.out.print(s + "\t");
}
System.out.println();
//迭代删除
for (Iterator<String> iterator = set.iterator(); iterator.hasNext(); ) {
String next = iterator.next();
if (next.length() < 5 ){
iterator.remove();
}
}
System.out.println(set); //[123456]
}
}
package com.bjpowernode.set;
import java.util.HashSet;
import java.util.Objects;
/**
* HashSet
*/
public class Test03 {
public static void main(String[] args) {
//创建HashSet集合存储Stuent对象
HashSet<Student> hashSet = new HashSet<>();
Student stu = new Student("zhangsan", 98);
hashSet.add(stu);
hashSet.add(new Student("lisi", 85));
hashSet.add(new Student("wangwu", 55));
hashSet.add(new Student("zhaoliu", 35));
hashSet.add(new Student("chenqi", 95));
hashSet.add(new Student("feifei", 75));
for (Student student : hashSet) {
System.out.println(student);
}
/*
在student类中没有重写equals()/hashCode(), 程序运行后, 存储顺序: si, liu, san, fei, wu, qi
在Student类中重写equals()/hashCode()方法, 存储顺序: si, fei, liu, san, qi , wu
HashSet集合中元素的存储位置与 对象的哈希码有关, 在还有Hash字样的集合中,元素的存储位置都与元素的hashCode()返回值有关
*/
System.out.println( hashSet.contains(stu) ); //true
stu.score = 9;
System.out.println("----------修改了zhangsan对象的score属性后-------");
for (Student student : hashSet) {
System.out.println(student);
}
//没有给stu重新赋值, stu对象还保存在hashSet集合中
System.out.println( hashSet.contains(stu) );
/*
HashSet集合中的元素存储位置与对象的哈希码有关.
在执行第15行把stu添加到hashSet集合中时, 会根据stu对象的hashCode()返回值计算出一个存储位置A, 即把stu对象保存到hashSet集合的A位置上
在第32行修改了stu对象的score属性, stu对象的hashcode()计算由name属性与score属性一起计算出来的, 当stu对象的score属性重新赋值后, stu对象的hashCode()计算出来的哈希码可能与之前add()添加时stu对象的哈希码不一样了
再执行第38行hashSet.contains(stu)判断集合中是否包含stu对象时, 系统会根据 stu对象的hashCode()计算stu对象在hashSet的存储位置, 现在因为stu对象的hashCode()值发生变化 了, 计算出来一个新的存储位置B. 去hashSet集合中的B位置查找是否存在Stu对象, 当前Stu对象存储在A位置上, 来B位置查找,不存在
*/
hashSet.add(stu);
System.out.println("============================");
for (Student student : hashSet) {
System.out.println(student);
}
hashSet.add(stu); //元素重复了
Student stuX = new Student("zhangsan", 98);
System.out.println( hashSet.contains(stuX)); //false
}
static class Student{
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return score == student.score &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, score);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
}
package com.bjpowernode.set;
import java.util.Comparator;
import java.util.TreeSet;
/**
* 在TreeSet集合中存储自定义类型对象Student
* 1) TreeSet集合中判断两个元素是否相同,是根据 Comparator比较器的比较结果是否为0来判断的
*/
public class Test05 {
public static void main(String[] args) {
//1) 创建TreeSet集合存储Student对象, 在构造方法中指定根据学生成绩降序排序
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.score - o1.score;
}
});
treeSet.add( new Student("zhangsan", 60));
treeSet.add( new Student("lisi", 90));
treeSet.add( new Student("wangwu", 70));
treeSet.add( new Student("zhaoliu", 80));
treeSet.add( new Student("chenqi", 40));
treeSet.add( new Student("zhuba", 70));
/*2)
当前treeSet指定了Comparator比较器, 如果compare方法返回0表示两个元素相等
在第21行添加了wangwu对象成绩是70, 在24行添加zhuba同学时, 会比较zhuba同学的成绩与treeSet集合中元素的成绩是否相等, 现在zhuba同学的成绩与TreeSet集合中wangwu同学的成绩相等, compare()方法返回值为0, 就认为是相同的元素, Set集合不允许存储重复的元素
*/
for (Student student : treeSet) {
System.out.println( student );
}
Student stu = new Student("feifei", 90);
System.out.println( treeSet.contains(stu)); //true
//3) 创建TreeSet集合,不在构造方法指定Comparator, 要求存储元素的类实现Comparable接口
TreeSet<Student> treeSet2 = new TreeSet<>();
treeSet2.addAll(treeSet);
for (Student student : treeSet2) {
System.out.println(student);
}
}
static class Student implements Comparable<Student>{
String name;
int score;
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
// 当前对象的姓名 参数o对象的姓名
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
}