Set集合
-
概述
一个不包含重复元素的 collection。更确切地讲,set 不包含满足
e1.equals(e2)
的元素对
e1
和e2
,并且最多包含一个 null 元素。
HashSet
- HashSet存储字符串并遍历
public class HashSetDemo {
public static void main(String[] args) {
HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(33);
hashSet.add(22);
hashSet.add(11);
for (Integer integer : hashSet) {
System.out.println(integer);
}
}
}
- HashSet保证元素的唯一性
HashSet底层结构是哈希表,HashSet不是线程安全的,集合的元素可以是null。
当HashSet集合中存入一个元素时,HashSet会调用该对象的hashCode()方法来得到该对象的 hashCode值,然后根据hashCode值决定该对象在HashSet中存储的位置
HashSet集合判断两个元素相等的标准:
两个对象通过hashCode()方法比较相等,并且两个对象的equals()返回值也相等
结论:HashSet保证元素的唯一性是靠元素重写hashCode()和equals()方法来保证的,如果 不重写则无法保证
- HashSet存储自定义对象保证元素唯一性
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int hashCode() {
//为了尽可能的区分,我们给年龄随便乘以一个整数
return this.name.hashCode() + this.age * 13;
}
@Override
public boolean equals(Object 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;
}
}
public class HashSetDemo {
public static void main(String[] args) {
HashSet<Student> hashSet = new HashSet<>();
Student s1 = new Student("张三",33);
Student s2 = new Student("张四",44);
Student s3 = new Student("张五",55);
Student s4 = new Student("张五",55);//由于重写了equals和hashcode方法,则只会 输出一个“张五”,55
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(s4);
for (Student student : hashSet) {
System.out.println(student.getName()+student.getAge());
}
}
}
- HashSet存储自定义对象保证元素唯一性图解
TreeSet
-
特点:
元素唯一,并且可以对元素进行排序
自然排序和使用比较器排序
-
TreeSet存储Integer类型的元素并遍历
public class bJTreeSet { public static void main(String[] args) { TreeSet<Integer> treeSet = new TreeSet<>(); treeSet.add(20); treeSet.add(25); treeSet.add(13); treeSet.add(2); treeSet.add(56); treeSet.add(29); treeSet.add(11); treeSet.add(78); treeSet.add(1); for (Integer integer : treeSet) { System.out.println(integer); }
注意:使用TreeSet集合进行元素的自然排序,那么要求这个元素必须实现Comparable接口,否则无 法进行自然排序;保证元素唯一性是靠comparTo方法的返回值来确定,如果返回0,表示两个 元素相等,则不重复存储
-
TreeSet存储自定义对象并遍历练习
注意自然排序 此对象 必须实现Comparable接口 否则报错
public class TreeSetDemo { public static void main(String[] args) { TreeSet<Student> treeSet = new TreeSet<>(); Student s1 = new Student("张三",33); Student s2 = new Student("张四",44); Student s3 = new Student("张五",55); treeSet.add(s1); treeSet.add(s2); treeSet.add(s3); for (Student student : treeSet) { System.out.println(student.getName()+student.getAge()); } } } public class Student implements Comparable<Student> { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } 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 String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Student o) { //先比较年龄 int num = this.age - o.age; //年龄相同再比较姓名 int num2=(num==0)?this.name.compareTo(o.name):num; return num2; } }
-
TreeSet存储自定义对象并遍历练习,比较器排序
在创建TreeSet对象的时候,传递一个比较器对象
public class TreeSetDemo2 { public static void main(String[] args) { TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num1 =s1.getAge() - s2.getAge() ; int num2 = num1 == 0 ? (s1.getName().compareTo(s2.getName())) : num1; return num2; } }); treeSet.add(new Student("张三",33)); treeSet.add(new Student("张四",44)); treeSet.add(new Student("张五",55)); for (Student student : treeSet) { System.out.println(student.getName()+student.getAge()); } } }
-
键盘录入学生信息按照总分排序后输出在控制台
需求:键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台。
/** * 步骤: * a: 自定义一个学生类 * b: 创建一个TreeSet集合对象(使用比较器进行排序) * c: 键盘录入学生的数据,然后把学生的数据封装成一个学生对象,把学生对象添加到集合中 * d: 遍历集合 */ public class StudentTest { public static void main(String[] args) throws IOException { TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num=s2.getSum()-s1.getSum(); int num2=num==0?s1.getName().compareTo(s2.getName()):num; return num2; } }); for (int i = 1; i <= 3; i++) { Student student = new Student(); Scanner sc = new Scanner(System.in); System.out.println("请输入第"+i+"个学生姓名"); String name = sc.nextLine(); student.setName(name); System.out.println("请输入第"+i+"个学生的语文成绩"); int chineseScore = sc.nextInt(); student.setChinese(chineseScore); System.out.println("请输入第"+i+"个学生的数学成绩"); int mathScore = sc.nextInt(); student.setMath(mathScore); System.out.println("请输入第"+i+"个学生的英语成绩"); int englishScore = sc.nextInt(); student.setEnglish(englishScore); treeSet.add(student); } System.out.println("学习信息从高到低排序如下:"); System.out.println("姓名\t语文成绩\t数学成绩\t英语成绩\t总分"); for (Student s : treeSet) { System.out.println(s.getName() + "\t\t" +s.getChinese() + "\t\t" + s.getMath() + "\t\t" + s.getEnglish()+"\t"+s.getSum()); } } public class Student { private String name; private int chinese; private int math; private int english; public Student() { } public Student(String name, int chinese, int math, int english) { this.name = name; this.chinese = chinese; this.math = math; this.english = english; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChinese() { return chinese; } public void setChinese(int chinese) { this.chinese = chinese; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } public int getSum(){ return this.chinese+this.math+this.english; } }
-