hashcode

day11

孤街浪徒 提交于 2019-12-28 11:29:07
面向对象 Object类 Object:所有类的根类,Object是不断抽取而来的,具备着所有对象都具备的共性内容。 常用的共性内容 : equals方法 指示其他某个对象是否与此对象“相等”。 class Person { private int age ; Person ( int age ) { this . age = age ; } } class ObjectDemo { main ( ) { Person p1 = new Person ( 20 ) ; Person p2 = new Person ( 20 ) ; Person p3 = p1 ; System . out . println ( p1 == p2 ) ; System . out . println ( p1 . equals ( p2 ) ) ; System . out . println ( p1 . equals ( p3 ) ) ; //false,false,true,对象的比较是比较地址是否一致 } } hashCode方法 返回该对象的哈希码值 class ObjectDemo { main ( ) { Person p1 = new Person ( 20 ) ; Person p2 = new Person ( 20 ) ; System . out . println (

Is it possible to combine hash codes for private members to generate a new hash code?

◇◆丶佛笑我妖孽 提交于 2019-12-28 05:34:26
问题 I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable. The code should be the result of combining the hash codes of a small collection of strings. The hash codes will be part of generating a cache key, so ideally they should be unique however the number of possible values that are being hashed is small so I THINK probability is in my favour here. Would something like this be sufficient AND is there a

two unequal objects with same hashcode

☆樱花仙子☆ 提交于 2019-12-28 05:23:04
问题 Hashcode() and equals() concept is 1) If two Objects are equal according to equal(), then calling the hashcode method on each of those two objects should produce same hashcode. and other one is 2) It is not required that if two objects are unequal according to the equal(), then calling the hashcode method on each of the two objects must produce distinct values. I tried and understood first one and this is the code for first point. public class Test { public static void main(String[] args) {

Internal implementation of java.util.HashMap and HashSet

雨燕双飞 提交于 2019-12-28 05:10:42
问题 I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet . Following are the doubts popping in my mind for a while: Whats is the importance of the @Override public int hashcode() in a HashMap/HashSet? Where is this hash code used internally? I have generally seen the key of the HashMap be a String like myMap<String,Object> . Can I map the values against someObject (instead of String) like myMap<someObject, Object> ? What all contracts do I need

hashCode uniqueness

房东的猫 提交于 2019-12-28 04:23:09
问题 Is it possible for two instances of Object to have the same hashCode() ? In theory an object's hashCode is derived from its memory address, so all hashCodes should be unique, but what if objects are moved around during GC? 回答1: Given a reasonable collection of objects, having two with the same hash code is quite likely. In the best case it becomes the birthday problem, with a clash with tens of thousands of objects. In practice objects a created with a relatively small pool of likely hash

HashMap和HashTable

倾然丶 夕夏残阳落幕 提交于 2019-12-28 03:07:00
JAVA的基础知识:数据结构(Map,List,Set等),设计模式,算法,线程相关,IO/NIO,序列化等等 其次是高级特征:反射机制,并发与锁,JVM(GC策略,类加载机制,内存模型)等等 举个例子 就比如问你:HashMap 是不是有序的? 你回答不是有序的。那面试官就会可能继续问你,有没有有序的Map实现类呢? 你如果这个时候说不知道的话,那这块问题就到此结束了。如果你说有TreeMap和LinkedHashMap。 那么面试官接下来就可能会问你,TreeMap和LinkedHashMap是如何保证它的顺序的? 如果你回答不上来,那么到此为止。 如果你说TreeMap是通过实现SortMap接口,能够把它保存的键值对根据key排序,基于红黑树,从而保证TreeMap中所有键值对处于有序状态。 LinkedHashMap则是通过插入排序(就是你put的时候的顺序是什么,取出来的时候就是什么样子)和访问排序(改变排序把访问过的放到底部)让键值有序。 1、为什么用HashMap? HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射 HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和链表的寻址修改 HashMap是非synchronized,所以HashMap很快 HashMap可以接受null键和值

How to implement hashCode and equals method

给你一囗甜甜゛ 提交于 2019-12-28 02:51:10
问题 How should I implement hashCode() and equals() for the following class in Java? class Emp { int empid ; // unique across all the departments String name; String dept_name ; String code ; // unique for the department } 回答1: in Eclipse right mouse click-> source -> generate hashCode() and equals() gives this: /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (code == null ? 0 : code.hashCode()

How does Java order items in a HashMap or a HashTable?

帅比萌擦擦* 提交于 2019-12-27 13:43:07
问题 I was wondering how Java orders items in the Map ( HashMap or Hashtable ) when they are added. Are the keys ordered by the hashcode, memory reference or by allocation precedence...? It's because I've noticed same pairs in the Map are not always in the same order 回答1: java.util.HashMap is unordered; you can't and shouldn't assume anything beyond that. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Thinking in Java:容器深入研究

て烟熏妆下的殇ゞ 提交于 2019-12-27 04:56:07
1.虚线框表示Abstract类,图中大量的类的名字都是以Abstract开头的,它们仅仅是部分实现了特定接口的工具,因此创建时能够选择从Abstract继承。 Collections中的实用方法:挑几个经常使用的: 1. reverse(List):逆转次序 2. rotate(List,int distance)全部元素向后移动distance个位置,将末尾元素循环到前面来(用了三次reverse) 3.sort(List,Comparator) 排序,可依照自己的Comparator 4.copy(List dest,List src) 复制元素(引用) 5.fill(List,T x)同Arrays一样。复制的是同一引用 6.disjoint(Collection。Collection) 两个集合没有不论什么元素时返回ture 7.frequency(Collection, Object x)返回Collection中等于x的元素个数 8.binarySearch()折半查找(要求有序) Collection的功能方法: boolean add(T) 可选方法 。若没将參数加入进容器,则false boolean addAll(Collection ) 可选方法 ,仅仅要加入了随意元素就true void clear() 可选方法 boolean contains(T)

java面试题汇总40个基础题

余生长醉 提交于 2019-12-27 03:08:22
1.&和&&的区别? &:逻辑与(and),运算符两边的表达式均为true时,整个结果才为true。 &&:短路与,如果第一个表达式为false时,第二个表达式就不会计算了。 2.在java中如何跳出当前的多重循环? 在循环语句外前面定义一个标号,然后在里层循环体的代码中使用带有标号的break语句,即可跳出循环。 3.最有效率的方法算出2X8等于几? 使用位运算,效率最高:2<<3,表示2向右移动了3位,就相当于2乘以2的3次方,结果:16。 4.”==”和equals方法究竟有什么区别? == : 表示两个变量的值是否相等,比较两个基本数据类型的数据或者引用变量,用==。 equals:用于比较两个独立对象的内容是否相同。字符串的比较也用equals。 5.Int和integer的区别? Int是Java的8中基本数据类型之一,integer是int的封装类。Int类型的默认值为0,integer默认值为null,所以区别在于,integer能区分出null值和0的区别。 6.三个与取整有关的方法: Math.ceil():表示向上取整;Math.ceil(11.3)=12;Math.ceil(-11.3)=-12。 Math.floor():表示向下取整;Math.floor(11.6)=12;Math.floor(-11.6)=-12。 Math.round()