重写eqlus与hashCode方法例子
来源:互联网 发布:oncity中山网络社区 编辑:程序博客网 时间:2024/06/07 06:33
package cn.itcastset;import java.util.HashSet;import java.util.Scanner;/* 需求: 接受键盘录入用户名与密码,如果用户名与密码已经存在集合中,那么就是视为重复元素,不允许添加到HashSet中。 */class User{String userName;String password;public User(String userName, String password) {super();this.userName = userName;this.password = password;}@Overridepublic String toString() {return "{ 用户名:"+this.userName+" 密码:"+ this.password+"}";}@Overridepublic boolean equals(Object obj) {User user = (User)obj;return this.userName.equals(user.userName)&&this.password.equals(user.password);}@Overridepublic int hashCode() { // abc 123 , 123 abcreturn userName.hashCode()+password.hashCode();}}public class Demo3 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);HashSet set = new HashSet(); );while(true){System.out.println("请输入用户名:");String userName = scanner.next();System.out.println("请输入密码:");String password = scanner.next();//创建一个对象User user = new User(userName, password);if(set.add(user)){System.out.println("注册成功...");System.out.println("当前的用户有:"+ set);}else{System.out.println("注册失败...");}}}}
阅读全文
0 0
- 重写eqlus与hashCode方法例子
- 重写hashCode与equals方法的作用
- 重写hashCode与equals方法的作用
- 重写hashCode与equals方法的作用
- 重写hashCode与equals方法的作用
- 一个重写equals()和hashCode()方法的例子
- 重写一下hashCode方法
- 重写 hashCode 方法
- 重写hashCode方法
- 如何重写hashcode方法
- 重写hashCode方法
- 重写hashCode方法
- 重写hashCode()方法
- 重写hashCode方法
- hashcode的重写方法
- 重写equals()与hashCode()
- 重写hashcode()与equal()
- 浅析HashSet中equals()与hashCode()方法的重写
- python学习一
- vmware安装报错: This product may not be installed on a computer that has Microsoft HyperV installed.
- TS流分析
- c#运行JS代码
- JAVA动态代理
- 重写eqlus与hashCode方法例子
- 解决Qt5.8加载qt_zh_CN.qm之后QMessageBox仍显示英文的问题
- C-ordered contiguous
- IntelliJ IDEA 开发Web应用图文教程
- Vue CheckBox 全选
- 将EXCEL导到oracle数据库
- RabbitMQ (五)主题(Topic)
- 数值的整数次方
- iframe+js实现页面全屏、淡入淡出切换