一、Set的使用
![](https://img2022.cnblogs.com/blog/2901531/202206/2901531-20220618190756446-365574859.png)
点击查看代码
@Test
public void test7(){
HashSet set = new HashSet();
set.add(456);
set.add(123);
set.add(123);
set.add("AA");
set.add(new Person("Twq",23));
set.add(new Person("Twq",23));
set.add("CC");
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行结果图
二、Set下的LinkedHashSet
![](https://img2022.cnblogs.com/blog/2901531/202206/2901531-20220619100907902-1839085787.png)
三、Set下的TreeSet
点击查看代码
@Test
public void test8(){
TreeSet set = new TreeSet();
//添加失败的原因就是不能添加不同类的对象
// treeSet.add(123);
// treeSet.add(456);
// treeSet.add("aaa");
set.add(32);
set.add(-12);
set.add(12);
set.add(123);
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行结果图
点击查看代码
@Test
public void test8(){
TreeSet set = new TreeSet();
//添加失败的原因就是不能添加不同类的对象
// treeSet.add(123);
// treeSet.add(456);
// treeSet.add("aaa");
//举例一:
// set.add(32);
// set.add(-12);
// set.add(12);
// set.add(123);
//举例二:
set.add(new Person("Twq",23));
set.add(new Person("Jerry",12));
set.add(new Person("Tom",30));
set.add(new Person("Twq",18));
set.add(new Person("Mack",32));
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
Person类代码
点击查看代码
package com.Tang.set;
public class Person implements Comparable{
private String name;
private int age;
public Person() {
}
public Person(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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//向collection接口的实现类的对象中添加数据obj时,要求obj所在的类要重写equals()方法
@Override
public boolean equals(Object o) {
System.out.println("Person equal()...");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int compareTo(Object o) {
if(o instanceof Person){
Person p = (Person) o;
// return this.name.compareTo(p.name);
int compare = this.name.compareTo(p.name);
if(compare != 0)//名字不相同,按名字从小到大排序
return compare;
else{//当名字相同时按年龄从小到大排序
return Integer.compare(this.age,p.age);
}
}
else{
throw new RuntimeException("输入数据有误");
}
}
// @Override
// public int hashCode() {
// int result = name != null ? name.hashCode() : 0;
// result = 31 * result + age;
// return result;
// }
}
运行结果图
ThreeSet的自定义排序
点击查看代码
@Test
public void test9(){
Comparator com = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
//按年龄从大到小排序
if(o1 instanceof Person && o2 instanceof Person){
Person p1 = (Person) o1;
Person p2 = (Person) o2;
return -Integer.compare(p1.getAge(),p2.getAge());
}
else
throw new RuntimeException("输入类型不一致");
}
};
TreeSet set = new TreeSet(com);//传入参数com使得其按照指定的排序来
set.add(new Person("Twq",23));
set.add(new Person("Jerry",12));
set.add(new Person("Tom",30));
set.add(new Person("Twq",18));
set.add(new Person("Mack",32));
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行结果图