zoukankan      html  css  js  c++  java
  • 【HashSet:重写比较方法示例一】

    package com.yjf.esupplier.common.test;
    
    import java.util.HashSet;
    
    /**
     * @author shusheng
     * @description
     * @Email shusheng@yiji.com
     * @date 2018/12/14 14:11
     */
    public class HashSetDemo {
        /**
         * 需求:存储自定义对象,并保证元素的唯一性
         * 要求:如果两个对象的成员变量值都相同,则为同一个元素。
         * 
         * 目前是不符合要求的:因为我们知道HashSet底层依赖的是hashCode()和equals() 方法
         * 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类的。
         * 这个时候,他们的哈希值是不一样的,根本就不会继续判断,执行了添加操作。
         */
        public static void main(String[] args) {
            //创建集合对象
            HashSet<Student> hs = new HashSet<Student>();
            Student s1 = new Student("林青霞", 27);
            Student s2 = new Student("王祖贤", 21);
            Student s3 = new Student("柳岩", 23);
            Student s4 = new Student("林青霞", 27);
            Student s5 = new Student("范冰冰", 32);
            Student s6 = new Student("林青霞", 27);
    
            //添加元素hs.add(s1);
            hs.add(s2);
            hs.add(s3);
            hs.add(s4);
            hs.add(s5);
            hs.add(s6);
    
            for (Student s : hs) {
                System.out.println(s.getName() + "---" + s.getAge());
            }
        }
    }
    
    class Student {
    
        private String name;
        private int age;
    
        public Student(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        /**
         * 如果对象的哈希值相同了,就会走equals()方法进行比较对象的成员变量是否相同
         * 如果不同,就添加到集合,如果相同,就不添加
         * 哈希表:是一个元素为链表的数组,综合了数组和链表的好处。如何优化代码:
         * 让对象的哈希值尽可能的不同。哈希值和哪些内容相关呢?
         * 和对象的成员变量值相关。
         * 所以,我们的最终解决方法就是把对象的成员变量值进行相加: 如果是基本类型,就直接加值。
         * 如果是引用类型,就加哈希值。
         */
        @Override
        public int hashCode() {
            return this.name.hashCode() + this.age * 113;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
    
            if (!(obj instanceof Student)) {
                return false;
            }
    
            Student s = (Student) obj;
            return this.name.equals(s.name) && this.age == s.age;
        }
    
    }
    终身学习者
  • 相关阅读:
    UVA 11149.Power of Matrix-矩阵快速幂倍增
    51nod 1137.矩阵乘法-矩阵乘法
    HDU 4920.Matrix multiplication-矩阵乘法
    HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
    HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
    POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)
    POJ 3041.Asteroids-Hungary(匈牙利算法)
    HDU 2063.过山车-Hungary(匈牙利算法)
    Codeforces 832 B. Petya and Exam-字符串匹配
    HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/10340897.html
Copyright © 2011-2022 走看看