(SortedSet)TerrSet
TreeSet:
特点:有序 唯一
数据结构:二叉树
TreeSet1:基本用法和特点
如何创建对象:
TreeSet<泛型>set=new TreeSet<>();
添加元素:
set.add(元素);
Collections.addAll(集合,元素....);
得到集合里的元素个数:
set.size();
判断集合里是否包含了指定的元素
set.contains(元素);
删除元素:
set.remove(元素);
得到集合里面的第一个元素
set.first();
得到集合里最后的元素
set.last();
得到集合第一个元素并且删除
set.pollFirst();
得到集合里最后一个元素
set.pollLast();
for +迭代器
TreeSet2:
TreeSet唯一:即使不是内存里面的同一个对象 也有可能视为相等对象,从而只能添加一个元素。
TreeSet3:
验证唯一
任何一个引用数据类型如果想要装进TreeSet集合的话都要求当前的类型自己就有比较规则 会排序
class Student implements Comparable<Studeng>{
@Overrid
public int compareTo(Student stu){
return stu.age-this.age;
}
}
TreeSet4:
当我们想要按照多个属性综合排序的时候 优先比较什么属性 就先描述这个属性不一样.....
TreeSet5:
在我们覆盖compareTo方法的时候尽量保证这个方法有机会返回0不然:
add(元素):认为所有元素都不一样——>唯一性被破坏
remove(元素):再也不能指定元素进行删除
contains(元素):永远返回false
TreeSet6:
当我们想要再遍历的过程中进行删除的话需要用迭代器的删除方法:car.remove();
TreeSet7:
当一个对象已经添加进TreeSet集合之后 不要随意的修改 那些参与排序的属性值
如果一定要改:先删除+再修改+再添加
TreeSet8:
TreeSet集合里面的根元素随时可以改变
TreeSet9:
如何脱离开一个类型指定这个类型的比较规则。
代码
import java.util.*;
public class TreeSetPuls{
public static void main(String[] args){
TreeSet<Integer> set = new TreeSet<>();
Collections.addAll(set,45,66,28,19,77);
System.out.println(set);
LinkedList<Integer>list=new LinkedList<>();
int x=set.size();
for(int i=0;i<x;i++){
System.out.print(set.pollLast());
}
}
}
import java.util.*;
public class TreeSetPuls{
public static void main(String[] args){
Ball b1 = new Ball(5,'蓝');
Ball b2 = new Ball(1,'黄');
Ball b3 = new Ball(8,'紫');
TreeSet<Ball>set=new TreeSet<>();
Collections.addAll(set,b1,b2,b3);
System.out.println(set);
}
}
class Ball implements Comparable<Ball>{
int number;
char color;
public Ball(int number,char color){
this.number=number;
this.color=color;
}
@Override
public int compareTo(Ball b){
return this.number-b.number;
}
@Override
public String toString(){
return number+" "+color;
}
}
```java
import java.util.*;
public class TreeSetPuls{
public static void main(String[] args){
Food f1 = new Food("猪耳朵拌黄瓜",23,2);
Food f2 = new Food("小鸡炖蘑菇",45,1);
Food f3 = new Food("82年的茅台",18000,0);
Food f4 = new Food("西红柿鸡蛋汤",25,3);
Food f5 = new Food("炒饼",7,4);
Food f6 = new Food("辣椒炒肉",23,1);
TreeSet<Food> set = new TreeSet<>();
Collections.addAll(set,f1,f2,f3,f4,f5,f6);
System.out.println(set);
for(Iterator<Food>car=set.iterator();car.hasNext();){
Food f=car.next();
if(f.name.equals("炒饼")){
car.remove();
}
}
System.out.println(set);
ArrayList<Food>list=new ArrayList<>();
for(Iterator<Food>car=set.iterator();car.hasNext();){
Food f=car.next();
if(f.type==0){
car.remove();
f.price=f.price*8/10;
list.add(f);
}
}
set.addAll(list);
System.out.println(set);
}
}
class Food implements Comparable<Food>{
String name;
int price;
int type;
public Food(String name,int price, int type){
this.name=name;
this.price=price;
this.type=type;
}
@Override
public int compareTo(Food f){
if(this.price!=f.price)return f.price-this.price;
if(this.type!=f.type)return this.type-f.type;
if(this.name!=f.name)return this.name.compareTo(f.name);
else return 1;
}
@Override
public String toString(){
if(type==0)return name+"菜"+" "+"酒水"+price;
if(type==1)return name+"菜"+" "+"热菜"+price;
if(type==2)return name+"菜"+" "+"凉菜"+price;
if(type==3)return name+"菜"+" "+"汤"+price;
else return name+"菜"+" "+"主食"+price;
}
}
```java
import java.util.*;
public class TreeSetPuls{
public static void main(String[] args){
TreeSet<String>set=new TreeSet<>(BJQ.getBjq());
Collections.addAll(set,"Jack","Anron","Lee","Andy");
System.out.println(set);
}
}
class BJQ implements Comparator<String>{
private BJQ(){}
private static BJQ bjq=new BJQ();
public static BJQ getBjq(){
return bjq;
}
@Override
public int compare(String x,String y){
return x.compareTo(y);
}
}
import java.util.*;
public class Exec75{
public static void main(String[] args){
TreeSet<Teacher>set=new TreeSet<>(BJQ.getBjq());
Teacher tea1=new Teacher("王老师",25,'女',4000);
Teacher tea2=new Teacher("刘老师",45,'男',6050);
Teacher tea3=new Teacher("张老师",55,'女',5000);
Teacher tea4=new Teacher("李老师",35,'男',4050);
Collections.addAll(set,tea1,tea2,tea3,tea4);
System.out.println(set);
//脱离开老师类 制定老师类的比较规则:
//优先按照工资降序排序
//工资一样的话 那么年龄升序排序
//年龄一样的话 按照性别升序排序
//性别也一样的话 按照姓名降序排序
//当所有属性都一样 舍弃元素 -》 单例模式实现
}
}
class Teacher {
String name;
int age;
char gender;
double salary;
public Teacher(String name,int age,char gender,double salary){
this.name=name;
this.age=age;
this.gender=gender;
this.salary=salary;
}
@Override
public String toString(){
return name+" "+gender+"今年"+age+"岁";
}
}
class BJQ implements Comparator<Teacher>{
public BJQ(){};
private static BJQ bjq=new BJQ();
public static BJQ getBjq(){
return bjq;
}
@Override
public int compare(Teacher x,Teacher y){
if(x.salary!=y.salary)return (int)(y.salary-x.salary);
if(x.age!=y.age) return x.age-y.age;
if(x.gender!=y.gender)return x.gender-y.gender;
if(x.name.equals(y.name)) return x.name.compareTo(y.name);
else return 1;
}
}