【158天】尚学堂高琪Java300集视频精华笔记(127)

一点小心得

我今天心里其实已经有决断了,后续快速开始执行。

程序员代码境界

  1. what:所有知识,都要知道是什么。

    1. 输入是什么?

    2. 输出是什么?

    3. 有何使用禁忌?

  2. why:20%常用知识,需要了解为什么。

    1. 为何需要输入这些?

    2. 为何输出是这些?(是什么逻辑实现了这样的输出)

    3. 使用禁忌来源于哪里?

  3. how:20%常用知识,需要了解怎么用。

    1. 需要实现什么功能时,要使用这个技术?

跨界

  1. 产品

  2. 营销

  3. 投资

第127集:引用类型_排序容器_TreeSet与TreeMap

  1. ArrayList容器本身是不可以排序的,需要借助工具类Collections.sort方法。

  2. TreeSet是Set接口的实现类,两个特点:数据元素可以排序且不可重复

  3. TreeSet和HashSet的区别:HashSet必须重写hashcode和equals方法,而TreeSet不用,它只要求它装的元素实现排序。(去重:比较等于0即重复)。

  4. 以下两种排序法二选一:

    1. 元素可排序:Comparable+c omaparaTo;

    package test127;
    
    public class Person {
        private final String name;
        private final int handsome;
        
    //    public void setName(String name){
    //        this.name = name;
    //    }
        
        public String getName(){
            return name;
        }
        
    //    public void setHandSome(int handsome){
    //        this.handsome = handsome;
    //    }
        
        public int getHandSome(){
            return handsome;
        }
        
        public Person(){
            name = null;
            handsome = 0;
        }
        
        public Person(String name,int handsome){
            this.name = name;
            this.handsome = handsome;
        }
        
        
        public String toString(){
            return "名称:"+this.name+" 颜值:"+this.handsome+"\n";
        }
        
        
    }
    
    package test127;
    
    import java.util.Comparator;
    import java.util.TreeSet;
    
    public class TestTreeSet {
        
        public static void main(String[] args){
            
            Person a = new Person("zhang",1000);
            Person b = new Person("zhang1",2000);
            Person c = new Person("zhang2",3000);
            Person d = new Person("zhang3",4000);
            Person e = new Person("zhang4",5000);
            Person f = new Person("zhang5",6000);
    
            
            TreeSet<Person> persons = new TreeSet<Person>(
                        new Comparator<Person>(){
                            public int compare(Person o1,Person o2){
                                return -(o1.getHandSome()-o2.getHandSome());
                            }    
                        }
                    
                    );
            
            persons.add(a);
            // TreeSet在添加数据时就完成了数据排序
            persons.add(b);
            persons.add(c);
            persons.add(d);
            persons.add(e);
            persons.add(f);
            
            System.out.println(persons);    
            //d.setHandSome(-1000);//这里一改值就会出问题,扰乱排序
            //e.setHandSome(6000);//也可能导致重复
            //杜绝的方法就是用final
            
            System.out.println(persons);
    
        
            
            
        }
    }
    1. 排序业务类:Comparator+compare

    package test127;
    
    public class Worker implements Comparable<Worker> {
        
        private String type;
        private int salary;
        
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public int getSalary() {
            return salary;
        }
        public void setSalary(int salary) {
            this.salary = salary;
        }
        
        public Worker(String type, int salary) {
            super();
            this.type = type;
            this.salary = salary;
        }
        public Worker() {
            
        }
        
        public int compareTo(Worker anotherWorker){
            return this.getSalary() - anotherWorker.getSalary();
        }
        
        public String toString(){
            return "名称:"+this.getType()+" 工资:"+this.getSalary();
        }
        
    }
    
    package test127;
    
    import java.util.TreeSet;
    
    public class TestTreeSet2 {
        
        public static void main(String[] args){
            
            Worker a = new Worker("zhang",1000);
            Worker b = new Worker("zhang1",2000);
            Worker c = new Worker("zhang2",3000);
        
    
            
            TreeSet<Worker> Workers = new TreeSet<Worker>();
            
            Workers.add(a);
            // TreeSet在添加数据时就完成了数据排序
            Workers.add(b);
            Workers.add(c);
            
            
            System.out.println(Workers);    
            //d.setHandSome(-1000);//这里一改值就会出问题,扰乱排序
            //e.setHandSome(6000);//也可能导致重复
            //杜绝的方法就是用final
            
            System.out.println(Workers);
    
        
            
            
        }
    }
    
  5. TreeMap

    1. Comparable+comparaTo

    package test127;
    
    import java.util.Comparator;
    import java.util.Set;
    import java.util.TreeMap;
    
    public class TestTreeMap {
        
        public static void main(String[] args){
            
            Person a = new Person("zhang",1000);
            Person b = new Person("zhang1",2000);
            Person c = new Person("zhang2",3000);
            Person d = new Person("zhang3",4000);
            Person e = new Person("zhang4",5000);
            Person f = new Person("zhang5",6000);
    
            
            TreeMap<Person,String> persons = new TreeMap<Person,String>(
                        new Comparator<Person>(){
                            public int compare(Person o1,Person o2){
                                return -(o1.getHandSome()-o2.getHandSome());
                            }    
                        }
                    );
            
            persons.put(a,"bj");
            persons.put(b,"bj");
            persons.put(c,"bj");
            persons.put(d,"bj");
            persons.put(e,"bj");
            persons.put(f,"bj");
            
            Set<Person> myPersons = persons.keySet();
            
            System.out.println(persons);    
            
        }
    }
    
    1. Comparator+compare

    package test127;
    
    import java.util.TreeMap;
    
    public class TestTreeMap2 {
        
        public static void main(String[] args){
            
            Worker a = new Worker("zhang",1000);
            Worker b = new Worker("zhang1",2000);
            Worker c = new Worker("zhang2",3000);
                
            TreeMap<Worker,String> Workers = new TreeMap<Worker,String>();
            Workers.put(a,"1");
            // TreeSet在添加数据时就完成了数据排序
            Workers.put(b,"1");
            Workers.put(c,"1");
            
            
            System.out.println(Workers.keySet());
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值