HashSet/TreeSet
- 创建一个student类
- 属性 age sex score name
- 创建10个student对象装入hashset中,通过设置student类保证age相差5岁以内 sex相同 name相同 score相差10以内为同一个学生 ,不能重复加.
package com.zy.homework;
public class Student {
private String name;
private String sex;
private int age;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name, String sex, int age, int score) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", score=" + score + "]";
}
@Override
public int hashCode() {
System.out.println("*");
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("&");
if (this == obj){
return true;
}
System.out.println("$");
if (obj == null){
return false;
}
Student other = (Student) obj;
if (name == null) {
if (other.name != null){
return false;
}
} else if (!name.equals(other.name)){
return false;
}
if (sex == null) {
if (other.sex != null){
return false;
}
} else if (!sex.equals(other.sex)){
return false;
}
System.out.println("@");
if(Math.abs(age - other.age)>=5||Math.abs(score - other.score)>=10){
return false;
}else{
return true;
}
}
}
package com.zy.homework;
import java.util.HashSet;
public class TestHashSet {
public static void main(String[] args) {
HashSet<Student> set = new HashSet<>();
set.add(new Student("张1","女",18,98));
set.add(new Student("张3","女",20,90));
set.add(new Student("张2","男",19,78));
set.add(new Student("张2","男",18,82));
set.add(new Student("张1","女",19,75));
set.add(new Student("张3","女",22,95));
set.add(new Student("张4","男",20,98));
set.add(new Student("张5","女",22,88));
set.add(new Student("张4","男",26,98));
set.add(new Student("张5","女",19,89));
for(Student s:set){
System.out.println(s);
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/36e4a552893fa11c1277b8724b7b3da8.png)
- 创建一个teacher类
- 属性age name salary
- 创建10个teacher对象 装入treeset中
- 通过设置teacher类 保证age大的在treeset前面 age相同 salary小的在treeset前面 salary相同 name小的在treeset前面
package com.zy.homework;
public class Teacher implements Comparable<Teacher>{
private String name;
private int age;
private int salary;
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 int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Teacher(String name, int age, int salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public Teacher() {
super();
}
@Override
public String toString() {
return "Teacher [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
@Override
public int compareTo(Teacher o) {
if(o.age-this.age==0){
if(o.salary-this.salary==0){
if(o.name.hashCode()-this.name.hashCode()==0){
return 0;
}else{
return o.name.hashCode()-this.name.hashCode();
}
}else{
return o.salary-this.salary;
}
}
return o.age-this.age;
}
}
package com.zy.homework;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
TreeSet<Teacher> set = new TreeSet<>();
set.add(new Teacher("张1",29,3000));
set.add(new Teacher("张1",29,3000));
set.add(new Teacher("张2",50,3300));
set.add(new Teacher("张2",45,3700));
set.add(new Teacher("张1",37,3500));
set.add(new Teacher("张3",29,3600));
set.add(new Teacher("张3",30,3200));
set.add(new Teacher("张4",45,3700));
set.add(new Teacher("张5",33,3800));
set.add(new Teacher("张4",29,3600));
set.add(new Teacher("张5",33,3000));
for(Teacher t:set){
System.out.println(t);
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2d9725c9773b80578c77de5d08a9e961.png)