hashcode

面试准备&总结-Java基础篇

匆匆过客 提交于 2019-12-17 14:43:03
  在vps的服务器到期了,在hw又不能访问,直接在博客园写笔记了。 基础篇 1. 集合类的继承关系,源码实现原理,初始大小和如何增长。 - list类初始大小10,加载因子为1,扩容到1.5+1。底层是个Object数组,调用 System.arraycopy进行拷贝。 - Vector同上,扩容倍数是两倍,是同步的,线程安全。 - HashMap初始大小16,加载因子0.75f,扩容到2倍。底层是数组+链表,调用resize()调整位置。 ConcurrentHashMap分段锁,相当于若干个hashtable。   -散列冲突:开放定址法,链地址法,最坏情况下如何优化。 - HashTable初始大小11,加载因子0.75f,扩容到2倍+1。同步,线程安全。 Hashtable不允许空 key空 value - TreeMap. 底层红黑树,能够保持有序,效率略低。 -阻塞队列LinkedBlockingQueue,非阻塞队列ConcurrentLinkedQueue,常用在线程pipe上。 指定大小可以提高性能。可能还会问Collections和Arrays中常用的方法。 2. interface和abstract class的区别 -接口可以多重实现,抽象类不能多重继承。 -抽象类中可以有 attribute,接口只能有 method。 -接口的方法必须没有实现

Java中的Set集合

孤者浪人 提交于 2019-12-17 14:22:23
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Set集合不允许包含相同的元素,如果试图把两个相同的元素加入同一个Set集合里面,则添加操作失败,add()方法返回false,且新元素不会被添加入。 1.HashSet是Set的接口的典型实现,大多数时候使用Set集合就是使用这个实现类,HashSet按Hash算法来存储集合中的元素,因此具有很好的存取和查找性能。HashSet具有以下特点,不能保证元素的排列顺序,可能和添加的顺序不同;HashSet不是同步的,如果有多个线程同是访问一个HashSet,则必须通过代码来保证其同步性;集合元素值可以是null。当向HashSet集合存入一个元素时,HashSet会调用该对象的hashcode()方法来得到该对象的hashCode值,然后提供该hashCode值决定该对象在HashSet中的存储位置。如果有两个元素通过equals()方法比较返回true,但他们的hashCode()方法返回值不相等,HashSet将会把他们存储在不同的位置,依然可以添加成功,也就是说HashSet集合判断两个元素相等的标准是两个对象通过equals()方法比较相等,并且两个对象的hashCode方法返回值也相等。 HashSet中每个能存储元素的“槽位”(solt)通常也称为“桶”(bucket)

Java - TreeSet and hashCode()

二次信任 提交于 2019-12-17 12:15:14
问题 I have a quick question about TreeSet collections and hashCode methods. I have a TreeSet and I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSet using the contains method. I have 2 distinct objects, each of which produce a distinct hashCode using my implementation of the hashCode method, example below: public int hashCode() { int hash = 7; hash = hash * 31 + anAttribute.hashCode(); hash = hash * 31 + anotherAttribute.hashCode(); hash = hash * 31 +

图解集合4:HashMap

陌路散爱 提交于 2019-12-17 11:39:31
初识HashMap 之前的List,讲了ArrayList、LinkedList,最后讲到了CopyOnWriteArrayList,就前两者而言,反映的是两种思想: (1)ArrayList以数组形式实现,顺序插入、查找快,插入、删除较慢 (2)LinkedList以链表形式实现,顺序插入、查找较慢,插入、删除方便 那么是否有一种数据结构能够结合上面两种的优点呢?有,答案就是HashMap。 HashMap是一种非常常见、方便和有用的集合,是一种键值对(K-V)形式的存储结构,下面将还是用图示的方式解读HashMap的实现原理, 四个关注点在HashMap上的答案 关 注 点 结 论 HashMap是否允许空 Key和Value都允许为空 HashMap是否允许重复数据 Key重复会覆盖、Value允许重复 HashMap是否有序 无序,特别说明这个无序指的是 遍历HashMap的时候,得到的元素的顺序基本不可能是put的顺序 HashMap是否线程安全 非线程安全 添加数据 首先看一下HashMap的一个存储单元Entry: static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; int hash; ... } 之前一篇写LinkedList的文章

Bit-shifting in Effective Java hashCode() implementation

余生颓废 提交于 2019-12-17 09:35:32
问题 I was wondering if someone could explain in detail what (int)(l ^ (l >>> 32)); does in the following hashcode implementation (generated by eclipse, but the same as Effective Java): private int i; private char c; private boolean b; private short s; private long l; private double d; private float f; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + i; result = prime * result + s; result = prime * result + (b ? 1231 : 1237); result = prime * result

Mutable objects and hashCode

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 09:33:39
问题 Have the following class: public class Member { private int x; private long y; private double d; public Member(int x, long y, double d) { this.x = x; this.y = y; this.d = d; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = (int) (prime * result + y); result = (int) (prime * result + Double.doubleToLongBits(d)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof

Why can hashCode() return the same value for different objects in Java?

淺唱寂寞╮ 提交于 2019-12-17 08:59:25
问题 A quote from the book I'm reading Head First Java: The point is that hashcodes can be the same without necessarily guaranteeing that the objects are equal, because the "hashing algorithm" used in the hashCode() method might happen to return the same value for multiple objects. Why might the hashCode() method return the same value for different objects? Does that not cause problems? 回答1: hashing an object means " finding a good, descriptive value (number) that can be reproduced by the very

[Java复习] 集合框架 Collection

╄→гoц情女王★ 提交于 2019-12-17 08:36:15
Q1 Collection java的集合以及集合之间的继承关系? 数组和链表的区别? 固定长度,连续内存,不能扩展,随机访问快,插入删除慢。链表相反 List, Set, Map的区别? List,Set继承Collection接口 List可以放重复数据,Set不能,Map是k-v对 List和Map的实现方式以及存储方式? ArrayList: 底层动态数组。随机访问快,增删慢,线程不安全。 扩容导致数组复制,批量删除会导致找两个集合交集,效率低。 LinkedList: 底层链表(双向列表)。增删快,查找慢,线程不安全。 遍历: 1.普通for循环,元素越多后面越慢 2.迭代器:每次访问,用游标记录当前位置 根据下标获取node,会根据index处于前半段还是后半段进行折半,提升效率。 HashMap: 散列表, 数组+链表+红黑树(JDK1.8) 默认16, 扩容2的幂 Q2 List ArrayList实现原理? 动态数组,默认10,扩容grow(minCapacity),增加到1.5倍 ArrayList和LinkedList的区别,以及应用场景? 1.动态数组和双向队列链表。 2.ArrayList( 实现了RandomAccess接口 )用for循环遍历优于迭代器,LinkedList则相反。 3.ArrayList在数组任意位置插入,或导致该位置后面元素重新排列

General advice and guidelines on how to properly override object.GetHashCode()

家住魔仙堡 提交于 2019-12-17 08:29:49
问题 According to MSDN, a hash function must have the following properties: If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals

what is the difference between == operator and equals()? (with hashcode() ???)

爱⌒轻易说出口 提交于 2019-12-17 06:17:07
问题 I was learning hashcode in more depth and figured that: 1. If you override equals(), you must override hashcode() too. 2. To find if 2 objects are same object, use == operator Given those 2 factors, in Java I was assuming that when == operator is used to compare if 2 instances are same or not, if(object1 == object2) is actually doing if(object1.hashcode() == object2.hashcode()) But it appears I was wrong by running the test below. public class Main { public static void main(String[] args){