HashSet中存放不重复元素
一.自定义对象存放在hashSet中,保证元素不重复。重写hashCode()和equals()方法
public class Student{
private String name;
private Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int hashCode() {
//return name.hashCode()+age;
//为了让hash值的范围大一些,我们这里可以乘以一个常数
return name.hashCode()+age*39;
}
@Override
public boolean equals(Object obj) {
if (this == obj){
//如果是同一个引用,不用比较了
return true;
}
if (!(obj instanceof Person)){
//如果类型不能转换,不用比较了
throw new ClassCastException("类型错误");
}
Student student = (Student) obj;
return this.name.equals(student.name) && this.age == student.age;
}
}
HashSet中存放不重复元素的更多相关文章
- Set中如何区分重复元素
Set接口常用实现类:HashSet和TreeSet HashSet区分重复元素: 先使用hashcode方法判断已经存在HashSet中元素的hashcode值和将要加入元素hashcode值是否相 ...
- HashSet中是如何判断元素是否重复的
HashSet不能添加重复的元素,当调用add(Object)方法时候, 首先会调用Object的hashCode方法判hashCode是否已经存在,如不存在则直接插入元素: 如果已存在则调用Obje ...
- 使用HashMap或Hashset优化使用循环判断字符串中是否含有重复元素
原本遇到判断字符串中是否含有重复元素的问题总是使用for循环遍历进行判断,这一方法则需要O(n3)的时间复杂度,如果本身方法处于几个循环中,就会指数倍增加时间复杂度.类似于如下代码: String[] ...
- c#:判断一个数组元素中否有重复元素
给定一个数组,判定该数组中是否有重复元素. 判定该数组中是否有重复元素总结出以下实现方案: using System; using System.Collections.Generic; using ...
- JS 验证数组中是否包含重复元素
验证JS中是否包含重复元素,有重复返回true:否则返回false 方案一. function isRepeat(data) { var hash = {}; for (var i in data) ...
- JS数组常用函数以及查找数组中是否有重复元素的三种常用方法
阅读目录: DS01:常用的查找数组中是否有重复元素的三种方法 DS02:常用的JS函数集锦 DS01.常用的查找数组中是否有重复元素的三种方法 1. var ary = new Array(&qu ...
- JS判断数组中是否有重复元素的方法
判断数组中是否有重复元素,最容易想到的方法是使用2重循环,逐个遍历,比较,但是这个是最慢,最笨的方法,百度得出了更好的方法. var ary = new Array("111",& ...
- 去除List<Object>集合中重复的元素(利用HashSet的特性---无重复元素)
import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator; public class Hashset ...
- 961 -尺寸2N阵列中的N重复元素
在一个A大小的数组中2N,有N+1独特的元素,这些元素中的一个重复N次. 返回重复N次的元素. 例1: 输入:[1,2,3,3] 输出:3 例2: 输入:[2,1,2,5,3,2] 输出:2 例3: ...
随机推荐
- window.location.href刷新页面
刷新当前页 window.location.href=window.location.href; 或者 window.location.href="当前URL",例如 window ...
- 【算法】LeetCode算法题-Remove Duplicates from Sorted Array
这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...
- March 07th, 2018 Week 10th Wednesday
Better later than never. 亡羊补牢,时犹未晚. Time and again all of us are told to complete the tasks assigned ...
- Apache Curator is a Java/JVM client library for Apache ZooKeeper
http://curator.apache.org/index.html Welcome to Apache Curator What is Curator? Curator n ˈkyoor͝ˌāt ...
- Jenkins+Ansible+Gitlab自动化部署三剑客-gitlab本地搭建
实际操作 准备linux初始环境 关闭防火墙 systemctl stop firewalld 开机自己关闭 systemctl disable firewalld 设置安全配置 为关闭 vim /e ...
- 配置数据库方言——hibernate
RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...
- Integer判断大于 == 127时的坑
在一次判断返回Interger类型的code, 用==结果, 没进去 Integer的值在-128到127时,Integer对象是在IntegerCache.cache产生,会复用已有对象,也就是说 ...
- eclipse导入maven项目, A resource exists with a different case: '/xxx'.
eclipse 导入maven 项目出现 这是由于你的workspace里有相同的项目, 这时在metadata里可以看到所有的project信息 只需在eclipse的package explore ...
- 有时间研究一下Maven打包插件细节
Maven工作分为多个阶段,具体阶段参考:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html ...
- 【angularjs】使用angular搭建项目,pc端实现网页中的内容不可复制
实现目标:不可复制页面内容 js: <script language="javascript"> if (typeof(document.onselectstart) ...