博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java随机4-Hashset
阅读量:4558 次
发布时间:2019-06-08

本文共 2497 字,大约阅读时间需要 8 分钟。

package com.broadengate.fangxing;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;/** * 应该都知道 hashset 不允许放入重复的数据,那么他是根据什么来判断是否是重复的数据呢? * 在hashset中用到了hash算法,因为hashset底层也是用的Hashmap来存取数据 * ,在hashmap底层则是用的数组来存取,而在hashmap放数据的时候会用到hashcode * ,这就是hashmap存入的数据没有顺序的原因,他就是根据hashcode来存放的 *  * 如果不重写hashcode 则默认是使用Object里面的,是根据对象的引用地址来生成的hashcode,所以在没创建一个对象的时候他的引用地址都不一样, *  */public class HashCodeTest {	public static void main(String[] args) {		HashCodeTest hashCodeTest = new HashCodeTest();		Collection coll = new /* ArrayList() */HashSet();		Person person = hashCodeTest.new Person("1", 1);		Person person1 = hashCodeTest.new Person("1", 1);		Person person2 = hashCodeTest.new Person("1", 1);		Person person3 = hashCodeTest.new Person("1", 1);		Person person4 = hashCodeTest.new Person("1", 1);		Person person5 = hashCodeTest.new Person("1", 1);		coll.add(person5);		coll.add(person4);		coll.add(person);		coll.add(person2);		coll.add(person3);		coll.add(person1);		// 如果重写了hashcode方法和eq方法, 如果这里把放入内存中的对象的值改变,则他的hashcode值就会变,		// jvm在根据hashcode算出来的区域去找person对象就找不到那个对象,所以就不能移除		person.setAge(2);		coll.remove(person);		System.out.println(coll.size());	}	class Person {		private String name;		private int age;		public Person() {		}		public Person(String name, int age) {			this.name = name;			this.age = age;		}		public String getName() {			return name;		}		public void setName(String name) {			this.name = name;		}		public int getAge() {			return age;		}		public void setAge(int age) {			this.age = age;		}		/**		 * @Override public int hashCode() { final int prime = 31; int result =		 *           1; result = prime * result + getOuterType().hashCode();		 *           result = prime * result + age; result = prime * result +		 *           ((name == null) ? 0 : name.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 (!getOuterType().equals(other.getOuterType())) return		 *           false; if (age != other.age) return false; if (name ==		 *           null) { if (other.name != null) return false; } else if		 *           (!name.equals(other.name)) return false; return true; }		 * 		 *           private HashCodeTest getOuterType() { return		 *           HashCodeTest.this; }		 **/	}}

转载于:https://www.cnblogs.com/changweihua/archive/2011/10/24/2223179.html

你可能感兴趣的文章
Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数
查看>>
Spring基础2
查看>>
【灵异短篇】这个夜晚有点凉
查看>>
一点小问题
查看>>
pytest 10 skip跳过测试用例
查看>>
MVC身份验证及权限管理
查看>>
It was not possible to find any compatible framework version
查看>>
关于8.0.15版本的mysql下载与安装
查看>>
Redis主从复制看这篇就够了
查看>>
洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth 题解
查看>>
(4.20)SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧...
查看>>
基本数据类型(数字和字符串)
查看>>
函数__装饰器
查看>>
linux system函数分析
查看>>
前端优化措施
查看>>
论学习汉语和学习编程的异同点
查看>>
linux img文件压缩及解压
查看>>
Linux 下的 scp
查看>>
理解同步,异步和延迟脚本
查看>>
MMS源码中异步处理简析
查看>>