zoukankan      html  css  js  c++  java
  • JAVA_HashSet

    package com.kk.Collection;

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;

    public class HashSetTest {
    public static void main(String[] args) {
    Set hashSet=new HashSet();
    hashSet.add(new Student(0,"kk"));
    hashSet.add(new Student(1,"mm"));
    hashSet.add(new Student(1,"mm"));//内存地址和值一样,Set将不会添加同样的对象
    Iterator it=hashSet.iterator();
    while(it.hasNext()){
    Student stu=(Student) it.next();
    System.out.println(stu.name);
    }
    }

    static class Student {
    int num;

    String name;

    public Student(int num, String name) {
    this.num = num;
    this.name = name;
    }

    @Override
    /**
    * 判断对象的内存地址是否一样
    */
    public int hashCode() {
    return num*name.hashCode();
    }

    @Override
    /**
    * 判断对象的值是否一样
    */
    public boolean equals(Object obj) {
    Student stu=(Student) obj;
    return this.num==stu.num && this.name.equals(stu.name);
    }
    }
    }
  • 相关阅读:
    阿里巴巴Java编码规范插件安装使用指南
    jhipster安装_Windows
    Linux 基本命令
    HTTPie命令介绍
    MySQL卸载
    Windows Phone8.1系统新特性
    SQL 游标知识整理
    浅析C#代理
    javascript 实现ajax
    jquery 之load post get
  • 原文地址:https://www.cnblogs.com/BigIdiot/p/2284966.html
Copyright © 2011-2022 走看看