其他分享
首页 > 其他分享> > 容器-TreeMap容器类(十九)

容器-TreeMap容器类(十九)

作者:互联网

容器-TreeMap容器类(十九)

  1. TreeMap容器类:TreeMap和HashMap同样实现了Map接口,所以,对于API的用法来说是没有任何区别的。HasMap效率高于TreeMap,TreeMap是可以对键进行排序的一种容器,在需要对键排序时,可选用TreeMap.TreeMap底层是基于红黑树实现的。

    在使用TreeMap时需要给定排序规则:

    • 元素自身实现比较规则
    • 通过比较器实现规则
  2. 元素自身实现比较规则

    • 创建Users类,给定比较规则

      import java.util.Objects;
      
      public class Users implements Comparable<Users> {
          private String username;
          private int userage;
      
          public Users(String username, int userage) {
              this.username = username;
              this.userage = userage;
          }
      
          public Users() {
          }
      
          @Override
          public boolean equals(Object o) {
              System.out.println("equals............");
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
              Users users = (Users) o;
              return userage == users.userage &&
                      Objects.equals(username, users.username);
          }
      
          @Override
          public int hashCode() {
              return Objects.hash(username, userage);
          }
      
          public String getUsername() {
              return username;
          }
      
          public void setUsername(String username) {
              this.username = username;
          }
      
          public int getUserage() {
              return userage;
          }
      
          public void setUserage(int userage) {
              this.userage = userage;
          }
      
          @Override
          public String toString() {
              return "Users{" +
                      "username='" + username + '\'' +
                      ", userage=" + userage +
                      '}';
          }
      
          //定义比较规则
          //返回的是正数:大,负数:小 ,0:相等
          @Override
          public int compareTo(Users o) {//o 参数的类型取决于 Comparable<Users>的泛型类型
              //通过年龄去定义比较规则
              if (this.userage >o.getUserage()){//当前的年龄比传进来的年龄大,则返回正数,
                  return 1;                       //这是由小到大的规则
              }
              //年龄相同则通过名字的排序来进行比较
              if (this.userage == o.getUserage()){
                  //名字是字符串,我们怎么来比较,因为Stringl类已经实现了compareTo的方法进行排序了
                  //所以,我们直接调用compareTo就可以了
                 return this.username.compareTo(o.getUsername());
              }
              return -1;//当前的年龄小传进来的年龄大,则返回负数
                          //相等的话,谁在谁前,谁在谁后,就没有关系了
          }
      }
      
      
    • 创建TreeMapTest类,实现排序

      import java.util.Map;
      import java.util.Set;
      import java.util.TreeMap;
      
      public class TreeMapTest {
          public static void main(String[] args) {
              //实例化TreeMap
              Map<Users,String> map=new TreeMap<>();
              Users u1=new Users("oldlu",18);
              Users u2=new Users("admin",22);
              Users u3=new Users("sxt",22);
      
              //添加元素,u1整个K-V结构当成是K,"oldlu"现在是V
              map.put(u1,"oldlu");
              map.put(u2,"admin");
              map.put(u3,"sxt");
              Set<Users> keys=map.keySet();
              for (Users key:keys){
                  System.out.println(key+"---------------"+map.get(key));
              }
          }
      }
      
  3. 通过比较器实现比较规则

    • 创建StudentComparator比较器,创建比较规则

      public class StudentComparator implements Comparator<Student> {
      
          //定义比较规则
          @Override
          public int compare(Student o1, Student o2) {
              //比较年龄比较,从小到大
              if (o1.getAge()>o2.getAge()){
                  return 1;
              }
      
              //如果年龄相同,则用名字进行排序
              if (o1.getAge()==o2.getAge()){
                  return o1.getName().compareTo(o2.getName());
              }
      
              return -1;
      
          }
      }
      
    • 创建TreeMapTest类,实现排序

      public class TreeMapTest {
          public static void main(String[] args) {
        
              Map<Student,String> treeMap=new TreeMap<>(new StudentComparator());
              Student s1=new Student("oldlu",18);
              Student s2=new Student("admin",22);
              Student s3=new Student("sxt",22);
              treeMap.put(s1,"oldlu");
              treeMap.put(s2,"admin");
              treeMap.put(s3,"sxt");
      
              Set<Student> keys1=treeMap.keySet();
              for (Student key:keys1){
                  System.out.println(key+"----------"+treeMap.get(key));
              }
      
          }
      }
      
      

标签:username,容器,return,Users,TreeMap,userage,十九,public
来源: https://blog.csdn.net/Xun_independent/article/details/114781500