Java中HashSet的使用
来源:互联网 发布:用java编写验证码 编辑:程序博客网 时间:2024/06/05 17:01
实验内容:
编写Student类, 将多个Student类型的对象放到HashSet中存放,并尝试删除和检验HashSet中是否含有某个对象。
编码实现:
package com.abc;import java.util.HashSet;class Student implements Comparable <Student>{static final int TableSize = 10459; //素数;private String Name;private String Id; //长度为10;public Student(String na, String id) {Name = na;Id = id;}public int hashCode() {int hashValue = Integer.parseInt(Id) % TableSize;return hashValue;}public boolean equals(Object obj) {//注意这儿是传入的Object obj 不是Student;Student stu = (Student)obj;if(this == obj)return true;if(stu.hashCode() != this.hashCode())return false;if(this.Id.equals(stu.Id))return true;return false;}public int compareTo(Student stu) {return 1;}public String toString() {return "Id is " + Id + "Name is " + Name;}}public class TestHashSet {public static void main(String []args) {HashSet <Student> StuHS = new HashSet<Student>();Student stu1 = new Student("小明", "1000000001");Student stu2 = new Student("小红", "1000000002");StuHS.add(stu1);StuHS.add(stu2);System.out.println(StuHS);System.out.println(StuHS.remove(new Student("小明", "1000000001")));StuHS.add(stu1);System.out.println(StuHS.remove(stu1));System.out.println(StuHS.contains(new Student("小红", "1000000002")));}}
阅读全文
0 0
- Java中HashSet的使用
- JAVA中Set集合--HashSet的使用
- java中HashSet的使用详解
- java HashSet的使用
- Java中ArrayList HashSet的使用 以及HashCode的用处
- Java中HashMap和HashSet的使用练习
- Java中HashSet、LinkedHashSet和TreeSet的使用
- java中hashset的用法
- java中HashSet的实现
- java 中 HashSet 的实现
- Java中HashSet的用法
- Java中HashSet的实现
- JAVA----hashset集合的使用
- Java中,使用HashSet<E>类
- Java 中HashSet的部分解释
- Java中HashSet HashTable HashMap的区别
- Java中HashSet的存储原理
- Java集合中HashSet的实现原理
- python sum([],axis=)
- 设计模式--装饰者模式
- Python可视化库
- 大规模分布式消息中间件简介
- 台式机win7系统安装CentOS7
- Java中HashSet的使用
- Python PIL 学习笔记(1)- Mac
- vue-preview 引用时报错
- Java后端工程师学习大纲
- class not found 或no class defined终结者
- windows 启动关闭oracle服务bat脚本
- 通过nfs挂载的文件系统不能进控制台
- 一个有趣的一一配对问题
- loadRunner12.02社区版