zoukankan      html  css  js  c++  java
  • java 17

    当我们想要创建一个集合,该集合里面的元素都具有唯一性时。会遇到两种情况:

      A:元素为String类型,可以直接用Hashset<String>集合来创建

          String类重写了hashCode()和equals()方法,所以,它就可以把内容相同的字符串去掉。只留下一个。

      B:当元素为自定义对象的时候,那么,就要在这个对象的类中重写hashCode()和equals()方法

      下图是思路:

    其实,扯了这么多,上面那些只要理解思路就行了。。。这玩意不用自己写

    在类上面 Alt+Shift+s ,再点h,就能生成相对应的重写的hashCode()和equls()方法了

    代码体现:

    首先创建一个对象类:

     1 package zl_HashCode;
     2 
     3 public class Animal {
     4     
     5     private String name;
     6     private String color;
     7     private int age;
     8     public Animal() {
     9         super();
    10         // TODO Auto-generated constructor stub
    11     }
    12     public Animal(String name, String color, int age) {
    13         super();
    14         this.name = name;
    15         this.color = color;
    16         this.age = age;
    17     }
    18     public String getName() {
    19         return name;
    20     }
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24     public String getColor() {
    25         return color;
    26     }
    27     public void setColor(String color) {
    28         this.color = color;
    29     }
    30     public int getAge() {
    31         return age;
    32     }
    33     public void setAge(int age) {
    34         this.age = age;
    35     }
    36     @Override
    37     public int hashCode() {
    38         final int prime = 31;
    39         int result = 1;
    40         result = prime * result + age;
    41         result = prime * result + ((color == null) ? 0 : color.hashCode());
    42         result = prime * result + ((name == null) ? 0 : name.hashCode());
    43         return result;
    44     }
    45     @Override
    46     public boolean equals(Object obj) {
    47         if (this == obj)
    48             return true;
    49         if (obj == null)
    50             return false;
    51         if (getClass() != obj.getClass())
    52             return false;
    53         Animal other = (Animal) obj;
    54         if (age != other.age)
    55             return false;
    56         if (color == null) {
    57             if (other.color != null)
    58                 return false;
    59         } else if (!color.equals(other.color))
    60             return false;
    61         if (name == null) {
    62             if (other.name != null)
    63                 return false;
    64         } else if (!name.equals(other.name))
    65             return false;
    66         return true;
    67     }
    68     
    69     
    70 }

    具体测试类:

     1 package zl_HashCode;
     2 /*
     3   HashSet集合存储自定义对象并遍历。如果对象的成员变量值相同即为同一个对象
     4  
     5      注意了:
     6          你使用的是HashSet集合,这个集合的底层是哈希表结构。
     7          而哈希表结构底层依赖:hashCode()和equals()方法。
     8          如果你认为对象的成员变量值相同即为同一个对象的话,你就应该重写这两个方法。
     9          如何重写呢?不同担心,自动生成即可。
    10 */
    11 import java.util.HashSet;
    12 
    13 public class HashCodeTest1 {
    14 
    15     public static void main(String[] args) {
    16         
    17         HashSet<String> hs = new HashSet<String>();
    18         //首先导入String类型的元素,看效果
    19         hs.add("阿猫");
    20         hs.add("阿狗");
    21         hs.add("花花");
    22         hs.add("阿猫");
    23         hs.add("阿狗");
    24         hs.add("草草");
    25         //遍历集合1
    26         for(String s1 : hs){
    27             //HashSet集合中存储String类型,元素会唯一性
    28             System.out.println(s1);//花花草草阿狗阿猫
    29         }
    30         
    31         HashSet<Animal> ha = new HashSet<Animal>();
    32         
    33         //再创建自定义对象的元素并导入
    34         Animal a1 = new Animal("荷兰猪","粉白",2);
    35         Animal a2 = new Animal("泰迪犬","棕色",1);
    36         Animal a3 = new Animal("哈士奇","白色",3);
    37         Animal a4 = new Animal("荷兰猪","粉白",2);
    38         Animal a5 = new Animal("泰迪犬","棕色",2);
    39         Animal a6 = new Animal("荷兰猪","黑白",2);
    40         
    41         ha.add(a1);
    42         ha.add(a2);
    43         ha.add(a3);
    44         ha.add(a4);
    45         ha.add(a5);
    46         ha.add(a6);
    47         
    48         for(Animal a : ha){
    49             System.out.println(a.getName()+"	"+a.getColor()+"	"+a.getAge());
    50             /*  对象类中重写了hashCode()和equals()方法,只让存储进唯一元素
    51                   若是不重写,则由于每个自定义对象的hashCode值不一样,所以都能存储进去
    52                       泰迪犬    棕色    1
    53             泰迪犬    棕色    2
    54             荷兰猪    粉白    2
    55             哈士奇    白色    3
    56             荷兰猪    黑白    2
    57              */
    58         }
    59     }
    60 
    61 }
    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    TapTap推广统计逻辑
    广告推广测试
    背压(Backpressure)机制
    工作相关资料
    ElasticSearch问题记录
    bfrd collector性能排查
    Ubuntu13.10下安装HADOOP
    Hadoop各商业发行版之比较
    Behave用户自定义数据类型
    Behave step matcher
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5903393.html
Copyright © 2011-2022 走看看