javaSE21/9/22

TreeSet

  • 基于HashCode计算元素存放位置
  • 基于排列顺序实现元素不=重复
public class Person implements Comparable<Person>{//必须先继承
    private String name ;
    private int age,phone;

    @Override
    public int compareTo(Person o) {//重写compareTo方法
        int n = this.getName().compareTo(o.getName());
        int n1= this.age-o.getAge();
        return n==0?n1:n;
    }

    public Person() {
    }

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

    @Override
    public String toString() {
        return name+":"+"年龄"+age+"电话"+phone;
    }

    @Override
    public int hashCode() {
        int n1 = this.name.hashCode();
        int n2  = age;
        int n3 = phone;
        return n1 + n2 + n3;
    }

    @Override
    public boolean equals(Object obj) {
        if (this==obj)
            return true;
        if (obj==null)
            return false;
        if (obj instanceof Person){
            Person p = (Person) obj;
            if (this.name.equals(p.getName())&&this.age==p.getAge()&&this.phone==p.phone){
                return true;
            }
        }
        return false;
    }

    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 getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }
}
/**
 * TreeSet中的元素必须实现Comparable接口
 * 否则程序无法判断将数据放在哪儿
 * compareTo方法返回值为0,认为是元素重复
 */
public class Test {
    public static  void main(String[] args){
        TreeSet<Person> treeSet = new TreeSet<>();
        Person p1 = new Person("莉莉",19,190);
        Person p2 = new Person("小明",20,110);
        Person p3 = new Person("小刚",19,180);
        treeSet.add(p1);
        treeSet.add(p2);
        treeSet.add(p3);
        System.out.println(treeSet.size());
        System.out.println(treeSet.toString());
    }
}

Comparator比较器

  • 可实现定制比较
  • 有比较器就不用实现Comparable接口
public class Test {
    public static  void main(String[] args){
        TreeSet<Person> persons = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                 int n1 = o1.getAge()-o2.getAge();
                 int n2 = o1.getName().compareTo(o2.getName());
                return n1==0?n2:n1;
            }
        });
        Person p1 = new Person("莉莉",19,190);
        Person p2 = new Person("小明",20,110);
        Person p3 = new Person("小刚",19,180);
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);
        System.out.println(persons.size());
        System.out.println(persons.toString());

    }
}

用Comparator接口实现字符串按照长度排序

public class Test {
    public static  void main(String[] args) {
        TreeSet<String> str = new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int n1 = o1.length()-o2.length();
                int n2 = o1.compareTo(o2);
                return n1==0?n2:n1;
            }
        });
        str.add("xyz");
        str.add("abcde");
        str.add("abc");
        str.add("f");
        System.out.println(str.size());
        System.out.println(str);
    }
}

Map集合

Map接口

  • 特点:存储一对数据(Key—Value),无序,无下标,键不可重复,值可以重复
上一篇:类和对象_对象的初始化和清理(下)


下一篇:检测设备跳转