• Java基础知识强化之集合框架笔记41:Set集合之HashSet存储自定义对象并遍历练习


    1. HashSet集合存储自定义对象并遍历。如果对象的成员变量值相同即为同一个对象

    注意了
      你使用的是HashSet集合,这个集合的底层是哈希表结构
      而哈希表结构底层依赖:hashCode()和equals()方法。
      如果你认为对象的成员变量值相同即为同一个对象的话,你就应该重写这两个方法
      如何重写呢?不同担心,自动生成即可。

    2. 代码示例:

    (1)Dog.java:

     1 package cn.itcast_03;
     2 
     3 public class Dog {
     4     private String name;
     5     private int age;
     6     private String color;
     7     private char sex;
     8 
     9     public Dog() {
    10         super();
    11     }
    12 
    13     public Dog(String name, int age, String color, char sex) {
    14         super();
    15         this.name = name;
    16         this.age = age;
    17         this.color = color;
    18         this.sex = sex;
    19     }
    20 
    21     public String getName() {
    22         return name;
    23     }
    24 
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28 
    29     public int getAge() {
    30         return age;
    31     }
    32 
    33     public void setAge(int age) {
    34         this.age = age;
    35     }
    36 
    37     public String getColor() {
    38         return color;
    39     }
    40 
    41     public void setColor(String color) {
    42         this.color = color;
    43     }
    44 
    45     public char getSex() {
    46         return sex;
    47     }
    48 
    49     public void setSex(char sex) {
    50         this.sex = sex;
    51     }
    52 
    53     @Override
    54     public int hashCode() {
    55         final int prime = 31;
    56         int result = 1;
    57         result = prime * result + age;
    58         result = prime * result + ((color == null) ? 0 : color.hashCode());
    59         result = prime * result + ((name == null) ? 0 : name.hashCode());
    60         result = prime * result + sex;
    61         return result;
    62     }
    63 
    64     @Override
    65     public boolean equals(Object obj) {
    66         if (this == obj)
    67             return true;
    68         if (obj == null)
    69             return false;
    70         if (getClass() != obj.getClass())
    71             return false;
    72         Dog other = (Dog) obj;
    73         if (age != other.age)
    74             return false;
    75         if (color == null) {
    76             if (other.color != null)
    77                 return false;
    78         } else if (!color.equals(other.color))
    79             return false;
    80         if (name == null) {
    81             if (other.name != null)
    82                 return false;
    83         } else if (!name.equals(other.name))
    84             return false;
    85         if (sex != other.sex)
    86             return false;
    87         return true;
    88     }
    89 
    90 }

    (2)DogDemo.java:

     1 package cn.itcast_03;
     2 
     3 import java.util.HashSet;
     4 
     5 /*
     6  * HashSet集合存储自定义对象并遍历。如果对象的成员变量值相同即为同一个对象
     7  * 
     8  * 注意了:
     9  *         你使用的是HashSet集合,这个集合的底层是哈希表结构。
    10  *         而哈希表结构底层依赖:hashCode()和equals()方法。
    11  *         如果你认为对象的成员变量值相同即为同一个对象的话,你就应该重写这两个方法。
    12  *         如何重写呢?不同担心,自动生成即可。
    13  */
    14 public class DogDemo {
    15     public static void main(String[] args) {
    16         // 创建集合对象
    17         HashSet<Dog> hs = new HashSet<Dog>();
    18 
    19         // 创建狗对象
    20         Dog d1 = new Dog("秦桧", 25, "红色", '男');
    21         Dog d2 = new Dog("高俅", 22, "黑色", '女');
    22         Dog d3 = new Dog("秦桧", 25, "红色", '男');
    23         Dog d4 = new Dog("秦桧", 20, "红色", '女');
    24         Dog d5 = new Dog("魏忠贤", 28, "白色", '男');
    25         Dog d6 = new Dog("李莲英", 23, "黄色", '女');
    26         Dog d7 = new Dog("李莲英", 23, "黄色", '女');
    27         Dog d8 = new Dog("李莲英", 23, "黄色", '男');
    28 
    29         // 添加元素
    30         hs.add(d1);
    31         hs.add(d2);
    32         hs.add(d3);
    33         hs.add(d4);
    34         hs.add(d5);
    35         hs.add(d6);
    36         hs.add(d7);
    37         hs.add(d8);
    38 
    39         // 遍历
    40         for (Dog d : hs) {
    41             System.out.println(d.getName() + "---" + d.getAge() + "---"
    42                     + d.getColor() + "---" + d.getSex());
    43         }
    44     }
    45 }

    运行效果如下:

  • 相关阅读:
    洛谷P2831 愤怒的小鸟
    2017-10-7 清北刷题冲刺班p.m
    2017-10-7 清北刷题冲刺班a.m
    2017-10-6 清北刷题冲刺班p.m
    2017-10-5 清北刷题冲刺班p.m
    2017-10-6 清北刷题冲刺班a.m
    2017-10-5 清北刷题冲刺班a.m
    2017-10-4 清北刷题冲刺班p.m
    2017-10-4 清北刷题冲刺班a.m
    题目
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4857554.html
Copyright © 2020-2023  润新知