package com.sf.day18;

import org.junit.Test;

import java.util.Hashtable;
import java.util.TreeMap;

/**
 * 自然排序     如果年龄相等比较姓名unicode
 */
public class User implements Comparable{
    private int age;
    private String name;

    public User() {
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if(this == o){
            return 0;
        }
        if(o instanceof User){
            User user = (User) o;
           int i = this.age - user.age;
            if (i != 0) {
            return i;
            }else {
                return this.name.compareTo(user.name);
            }
            }
         throw  new RuntimeException("类型不匹配");
        }


    public static void main(String[] args) {
        /**
         * treeMap  实现自然排序 是根据key（set集合）进行排序   treeset  实现自然排序
         */
        TreeMap<Object,Object> map = new TreeMap<>();
//        map.put("CC",45);
//        map.put("MM",78);
//        map.put("DD",56);
//        map.put("GG",89);
//        map.put("JJ",99);
//        System.out.println(map);
        map.put(new User(12,"zhangsan"),12);
        map.put(new User(10,"wang"),12);
        map.put(new User(15,"li"),12);
        map.put(new User(19,"qian"),12);
        map.put(new User(10,"jin"),12);
        System.out.println(map);

    }


}
