• 关于 TreeMap 和 HashMap 的去重操作


    HashMap:

      HashMap中的key去重是要重写equals跟hashCode实现

    测试代码:

      重写了equals跟hashCode后map的大小是1、如果没有重写map的大小是2。

    说明重写了equals跟hashCode后将HashMap集合的key进行了去重操作!

    import java.util.*;
    
    public class MapDemo {
        public static void main(String[] args) {
            Map map = new HashMap();
            map.put(new User(1, "张志喜"), "one");
            map.put(new User(1, "张志喜"), "two");
    
            System.out.println(map.size());
        }
    }
    
    class User {
        private int id;
        private String name;
    
        public User() {
        }
    
        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof User)) return false;
    
            User user = (User) o;
    
            if (getId() != user.getId()) return false;
            return getName() != null ? getName().equals(user.getName()) : user.getName() == null;
        }
    
        @Override
        public int hashCode() {
            int result = getId();
            result = 31 * result + (getName() != null ? getName().hashCode() : 0);
            return result;
        }
    }

    TreeMap:

      TreeMap的去重有两种方式:

        1、实现Comparable 并重写compareTo方法

        2、单独写一个比较器

    1、实现Comparable 并重写compareTo方法:

     1 import java.util.*;
     2 
     3 public class MapDemo {
     4     public static void main(String[] args) {
     5         Map map = new TreeMap();
     6         map.put(new User(1, "张志喜"), "one");
     7         map.put(new User(1, "张志喜"), "two");
     8 
     9         System.out.println(map.size());
    10     }
    11 }
    12 
    13 class User implements Comparable<User> {
    14     private int id;
    15     private String name;
    16 
    17     public User() {
    18     }
    19 
    20     public User(int id, String name) {
    21         this.id = id;
    22         this.name = name;
    23     }
    24 
    25     public int getId() {
    26         return id;
    27     }
    28 
    29     public void setId(int id) {
    30         this.id = id;
    31     }
    32 
    33     public String getName() {
    34         return name;
    35     }
    36 
    37     public void setName(String name) {
    38         this.name = name;
    39     }
    40 
    41     @Override
    42     public boolean equals(Object o) {
    43         if (this == o) return true;
    44         if (!(o instanceof User)) return false;
    45 
    46         User user = (User) o;
    47 
    48         if (getId() != user.getId()) return false;
    49         return getName() != null ? getName().equals(user.getName()) : user.getName() == null;
    50     }
    51 
    52     @Override
    53     public int hashCode() {
    54         int result = getId();
    55         result = 31 * result + (getName() != null ? getName().hashCode() : 0);
    56         return result;
    57     }
    58 
    59     public int compareTo(User o) {
    60         if (o.id == this.id && o.name.equals(this.name)) {
    61             return 0;
    62         }
    63         return -1;
    64     }
    65 }
  • 相关阅读:
    cf854B Maxim Buys an Apartment
    Snuke's Coloring 2-1
    P1087 FBI树
    Card Game for Three
    Many Formulas
    排队
    苹果消消乐(尺取法)
    猴子选大王(约瑟夫)
    进制转化
    UIProgress控件的属性和方法
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14281454.html
Copyright © 2020-2023  润新知