Skip to content

Latest commit

 

History

History
243 lines (172 loc) · 4.98 KB

16. TreeSet and TreeMap.md

File metadata and controls

243 lines (172 loc) · 4.98 KB

14. TreeSet and TreeMap

排序容器

1.TreeSet:数据元素可以排序且不可重复

对比Set接口:HashSet元素必须重写hashcode和equals方法。

去重:比较等于0即为重复

1)元素可以排序 java.lang.Comparable +compareTo

new TreeSet()

  1. 排序业务类 java.lang.Comparator +compare

new TreeSet(Comparator<? super E> comparator)

==在添加数据时排序,添加的数据不会改变原来的顺序,不要修改数据,否则可能重复==: 解决方法->将实体类中的属性定为final;


1. TreeSet

==实现方式一==:

Person.java:

package com.alexanderli95.col;

public class Person {
    private final String name;
    private final int ID;

    public Person(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }

    public Person(){
        this.name=null;
        this.ID=0;
    }

    public String getName() {
        return name;
    }


    public int getID() {
        return ID;
    }

    @Override
    public String toString() {
        return "姓名:"+this.name+" ID:"+this.ID+"\n";
    }
}

TreeSetDemo.java

package com.alexanderli95.col;

import java.util.TreeSet;

/**
 * 第一种方式
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        Person p1=new Person("a",1);
        Person p2=new Person("b",5);
        Person p3=new Person("c",2);
        Person p4=new Person("d",9);

        //存放到TreeSet容器之中,使用排序的业务类(如果不写的话需要元素可以排序,不能排序则需要提供业务类)
        TreeSet<Person> people=new TreeSet<Person>(
                new java.util.Comparator<Person>(){

                    @Override
                    public int compare(Person o1, Person o2) {
                        return o1.getID()-o2.getID();
                    }
                }
        );

        //TreeSet是在添加数据时进行排序
        people.add(p1);
        people.add(p2);
        people.add(p3);
        people.add(p4);

        System.out.println(people);
    }
}

==实现方式二==:

Worker.java

package com.alexanderli95.col;

public class Worker implements java.lang.Comparable<Worker>{
    private String type;
    private int salary;

    public Worker(String type, int salary) {
        this.type = type;
        this.salary = salary;
    }

    public Worker() {
    }

    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;
    }

    @Override
    public int compareTo(Worker o) {
        return this.salary-o.salary>0 ? 1 : (this.salary==o.salary ? 0 : -1);
    }

    @Override
    public String toString() {
        return "级别:"+this.type+" 工资:"+this.salary+"\n";
    }
}

TreeSetDemo02.java

package com.alexanderli95.col;

import java.util.TreeSet;

/**
 * 第二种方式
 */
public class TreeSetDemo02 {
    public static void main(String[] args) {
        Worker w1=new Worker("L1",3000);
        Worker w2=new Worker("L4",9000);
        Worker w3=new Worker("L2",5000);

        TreeSet<Worker> employees=new TreeSet<Worker>();
        employees.add(w1);
        employees.add(w2);
        employees.add(w3);

        System.out.println(employees);
    }
}

2.TreeMap

==实现方式一==:

TreeMapDemo.java

package com.alexanderli95.col;

import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        Person p1=new Person("a",1);
        Person p2=new Person("b",5);
        Person p3=new Person("c",2);
        Person p4=new Person("d",9);

        TreeMap<Person,String > people=new TreeMap<Person,String>(
                new java.util.Comparator<Person>(){

                    @Override
                    public int compare(Person o1, Person o2) {
                        return o1.getID()-o2.getID();
                    }
                }
        );

        people.put(p1,"swjtu");
        people.put(p2,"swjtu");
        people.put(p3,"swjtu");
        people.put(p4,"swjtu");

        System.out.println(people.keySet());

    }
}

==实现方式二==:

TreeMapDemo02.java

package com.alexanderli95.col;

import java.util.TreeMap;

public class TreeMapDemo02 {
    public static void main(String[] args) {
        Worker w1=new Worker("L1",3000);
        Worker w2=new Worker("L4",9000);
        Worker w3=new Worker("L2",5000);

        TreeMap<Worker,String> employees=new TreeMap<Worker,String>();
        employees.put(w1,"swjtu");
        employees.put(w2,"swjtu");
        employees.put(w3,"swjtu");

        System.out.println(employees.keySet());
    }
}