• HashSet的运用


    TestHashSet.java

    package com.sxt.set1;
    /*
     * Set接口   唯一:元素唯一(不重复)
     *                  无序:不是按照添加的顺序显示数据
     * 采用哈希表的方式存储
     * 根据哈希地址(取模)存储 调用equals()方法 若没有直接存储 若有不存储 保证了唯一性
     * 
     * Integer添加后  会自动排序(由于哈希表的存储方式,整数的哈希码是本身)
     * 自定义的类型 用HashSet方式存储需要重写 HashSet()方法、equals()方法、toString()方法
     */
    import java.util.HashSet;
    import java.util.Set;
    
    public class TestHashSet {
        public static void main(String[] args) {
            Set<Person> arr = new HashSet<>();
            arr.add(new Person("ccc", "女", 32.2));
            arr.add(new Person("ccc", "女", 32.2));
            System.out.println(arr);
        }
    }

    Person.java

    package com.sxt.set1;
    
    public class Person {
        private String name;
        private String sex;
        private double score;
        
        public Person() {
            super();
        }
        
        public Person(String name, String sex, double score) {
            super();
            this.name = name;
            this.sex = sex;
            this.score = score;
        }
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public double getScore() {
            return score;
        }
        public void setScore(double score) {
            this.score = score;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", sex=" + sex + ", score=" + score + "]";
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            long temp;
            temp = Double.doubleToLongBits(score);
            result = prime * result + (int) (temp ^ (temp >>> 32));
            result = prime * result + ((sex == null) ? 0 : sex.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person other = (Person) obj;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))
                return false;
            if (sex == null) {
                if (other.sex != null)
                    return false;
            } else if (!sex.equals(other.sex))
                return false;
            return true;
        }
        
    }
  • 相关阅读:
    An error happened during template parsing (template: "class path resource [templates/index.html]")(有效转)
    Netty线程模型及EventLoop
    社交网络大时代背景下的店铺社交群
    Shiro
    从n个数中取出m个最大数(复杂度最低)的最好的算法是什么?
    Shiro(基本)
    Shiro(转)
    一:Spring Boot 的配置文件 application.properties
    Spring Boot 基础,理论,简介
    收集整理Idea常用配置及插件
  • 原文地址:https://www.cnblogs.com/qingfengzhuimeng/p/6743065.html
Copyright © 2020-2023  润新知