hashcode

Altering hashCode of object inside of HashSet / HashMap

半腔热情 提交于 2020-01-04 06:24:20
问题 I am relatively new to Java and am puzzled about the following thing: I usually add objects to an ArrayList before setting its content. I.e., List<Bla> list = new ArrayList<>(); Bla bla = new Bla(); list.add(bla); bla.setContent(); // content influences hashCode This approach works great. I am concerned whether this approach will give me trouble when used with HashSet s or HashMap s. The internal hash table get set at the time the object is added. What will happen if setContent() gets called

HashSet.contains returns false when it shouldn't

家住魔仙堡 提交于 2020-01-04 05:22:28
问题 I have this code: public class Tray { private Set<Block> blocks; private int numColumns; private int numRows; //constructor public Tray (int numRows, int numColumns){ this.numColumns = numColumns; this.numRows = numRows; blocks = new HashSet<>(); } public boolean satisfiesGoal(Tray other){ Block thisBlock = this.blocks.iterator().next(); Block otherBlock = other.blocks.iterator().next(); boolean hashesEqual = thisBlock.hashCode() == otherBlock.hashCode(); // this is true boolean areEqual =

带妹学Java第十二天(至集合Set接口——TreeSet)

守給你的承諾、 提交于 2020-01-04 00:31:18
重点 1.Set接口的特点讲解 Set:接口 1.一个不包含重复元素的 collection。 2.最多包含一个 null 元素 3.一般使用它实现类:HashSet,LinkedHashSet,TreeSet 4.Set集合存和取的顺序不一样,【每一次取的顺序都可能不一样】 List:接口 1.List是可存储相同元素 2.List存的取的元素顺序是一样 2.HashSet 1.此类实现 Set 接口 2.由哈希表(实际上是一个 HashMap 实例)支持。 3.它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。 4.此类允许使用 null 元素。 Set遍历一、增强for循环 Set遍历二、迭代器 3.hashCode与equals的原理 1 .通过set集合的add方法添加元素时,内部会调用hashcode和equals方法 2. 如果比较的hashcode的值是一样的话,会调用equals方法,equals方法返回true,就不存元素,返回false就存元素 3. 如果比较的hashcode的值不一样,就直接存元素 4.hashCode与equals的优化 优化: Person -name -age 1.如果名字不一样,就不需要调用equals方法,从而优化代码 return this.name.hashCode() 2.hashCode():

Sorting ArrayList of Objects using Hashcode

醉酒当歌 提交于 2020-01-03 19:17:02
问题 I have an ArrayList of Objects(POJOs) that have an Id and another field. I have implemented the equals()/hashcode() override in the POJO for the Id field. When I compare two objects using the equals() method of the Object class, it works perfectly fine. However when I add these objects to an arraylist and implement the Collections.sort(arrListOfObjects); it gives me a classCastexception. I looked up and found that I need to implement a Comparator. This comparator also does something to equals

Apache Commons Lang3 Hashcode, Equals and ToString including Enums

淺唱寂寞╮ 提交于 2020-01-03 09:09:12
问题 We have few datatypes defined for our service response and request objects in a model. Recently we found a need of implementing ToString, HashCode and Equals on all such types to make use of these over comparison and assertions. Confirming from few source like What issues should be considered when overriding equals and hashCode in Java?, Right way to implement equals contract etc we followed implementing toString, equals and hashcode using org.apache.commons.lang3.builder.EqualsBuilder ,

Java中对List去重, Stream去重

假如想象 提交于 2020-01-03 08:34:35
每天学习一点点 编程PDF电子书、视频教程免费下载: http://www.shitanlife.com/code 问题 当下互联网技术成熟,越来越多的趋向去中心化、分布式、流计算,使得很多以前在数据库侧做的事情放到了Java端。今天有人问道,如果数据库字段没有索引,那么应该如何根据该字段去重?大家都一致认为用Java来做,但怎么做呢? 解答 忽然想起以前写过list去重的文章,找出来一看。做法就是将list中对象的hashcode和equals方法重写,然后丢到HashSet里,然后取出来。这是最初刚学Java的时候像被字典一样背写出来的答案。就比如面试,面过号称做了3年Java的人,问Set和HashMap的区别可以背出来,问如何实现就不知道了。也就是说,初学者只背特性。但真正在项目中使用的时候你需要确保一下是不是真的这样。因为背书没用,只能相信结果。你需要知道HashSet如何帮我做到去重了。换个思路,不用HashSet可以去重吗?最简单,最直接的办法不就是每次都拿着和历史数据比较,都不相同则插入队尾。而HashSet只是加速了这个过程而已。 首先,给出我们要排序的对象User @Data @Builder @AllArgsConstructor public class User { private Integer id; private String name; }

Hashcode comparison problem

巧了我就是萌 提交于 2020-01-03 02:28:31
问题 I have list of a an object which is termed as rule in our case, this object itself is a list of field for which I have to do hashcode comparison as we can't duplicate rule in the system. i.e Let say I have two Rules R1 and R2 with fields A & B. Now if values of A & B in R1 are 7 and 2 respectively. And in R2 it's 3 and 4 respectively then the process I have used to check the duplicity of Rules in the system that is hashcode comparison fails the method which I have used is for(Rule rule :

Order of items in a HashMap differ when the same program is run in JVM5 vs JVM6

怎甘沉沦 提交于 2020-01-02 02:52:09
问题 I have an application which displays a collection of objects in rows, one object = one row. The objects are stored in a HashMap. The order of the rows does not affect the functionality of the application (that is why a HashMap was used instead of a sortable collection). However I have noticed that the same application runs differently when run using two different versions of the Java Virtual Machine. The application is compiled using JDK 5, and can be run using either Java 5 or Java 6

Java集合之HashSet源码分析

[亡魂溺海] 提交于 2020-01-01 14:56:22
一、HashSet简介   HashSet是Set接口典型实现,它按照Hash算法来存储集合中的元素,具有很好的存取和查找性能。主要具有以下特点: 不保证set的迭代顺序 HashSet不是同步的,如果多个线程同时访问一个HashSet,要通过代码来保证其同步 集合元素值可以是null   当向HashSet集合中存入一个元素时,HashSet会调用该对象的hashCode()方法来得到该对象的hashCode值,然后根据该值确定对象在HashSet中的存储位置。在Hash集合中,不能同时存放两个相等的元素,而判断两个元素相等的标准是两个对象通过equals方法比较相等并且两个对象的HashCode方法返回值也相等。   下面的例子说明了上述特性: public class Person { String name; int age; 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

java集合HashMap、HashTable、HashSet详解

99封情书 提交于 2020-01-01 14:55:55
一、Set和Map关系 Set代表集合元素无序,集合元素不可重复的集合,Map代表一种由多个key-value组成的集合,map集合是set集合的扩展只是名称不同,对应如下 二、HashMap的工作原理 HashMap基于 hashing原理 ,通过put()和get()方法储存和获取对象。 put()方法: 它调用键对象的hashCode()方法来计算hashcode值,系统根据hashcode值决定该元素在bucket位置。如果两个对象key的hashcode返回值相同,那他们的存储位置相同,如果这两个Entry的key通过equals比较返回true,新添加Entry的value将覆盖集合中原有Entry的value,但key不会覆盖;如果这两个Entry的key通过equals比较返回false,新添加的Entry将与集合中原有Entry形成Entry链,而且新添加Entry位于Entry链的头部。put源码如下: public V put(K paramK, V paramV) { //如果key为空,调用putForNullKey方法 if (paramK == null) return putForNullKey(paramV); //根据key的keyCode计算Hash值 int i = hash(paramK.hashCode()); /