面試官:為什么不能將實(shí)數(shù)作為 HashMap 的 key?
往期熱門文章: 1、Redis 官方可視化工具,高顏值,功能太強(qiáng)大! 2、不好意思, Maven 該換了! 3、面試官 | Spring Boot 項(xiàng)目如何統(tǒng)一結(jié)果,統(tǒng)一異常,統(tǒng)一日志? 4、基于SpringBoot+MyBatis+Vue的音樂網(wǎng)站 5、聊聊接口優(yōu)化的幾種方法 來源:blog.csdn.net/qq_30219017/article/details/79689492
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
import java.util.HashMap;import java.util.Map;//class Point {// int x;// int y;// Point(int a, int b) { x = a; y = b; }//}public class Solution {public int maxPoints(Point[] points) {if (points.length <= 2) {return points.length;}int max = 2;for (int i = 0; i < points.length - 1; i++) {Map<Float, Integer> map = new HashMap<>(16);// 記錄垂直點(diǎn)數(shù); 當(dāng)前和Points[i]在一條線上的最大點(diǎn)數(shù); 和Points[i]垂直的點(diǎn)數(shù)int ver = 0, cur, dup = 0;for (int j = i + 1; j < points.length; j++) {if (points[j].x == points[i].x) {if (points[j].y != points[i].y) {ver++;} else {dup++;}} else {float d = (float)((points[j].y - points[i].y) / (double) (points[j].x - points[i].x));map.put(d, map.get(d) == null ? 1 : map.get(d) + 1);}}cur = ver;for (int v : map.values()) {cur = Math.max(v, cur);}max = Math.max(max, cur + dup + 1);}return max;}}
public static void main(String[] args) { int[][] vals = {{2,3},{3,3},{-5,3}}; Point[] points = new Point[3]; for (int i=0; i<vals.length; i++){ points[i] = new Point(vals[i][0], vals[i][1]); } Solution solution = new Solution(); System.out.println(solution.maxPoints(points));}System.out.println(0.0 == -0.0);
map.put(d, map.get(d) == null ? 1 : map.get(d) + 1);
public V get(Object key) {Node<K,V> e;return (e = getNode(hash(key), key)) == null ? null : e.value;}
static final int hash(Object key) {int h;return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);}
public native int hashCode();
public static void main(String[] args) {System.out.println(0.0 == -0.0);System.out.println(new Float(0.0).hashCode() ==new Float(-0.0).hashCode());}
在程序執(zhí)行期間,只要equals方法的比較操作用到的信息沒有被修改,那么對(duì)這同一個(gè)對(duì)象調(diào)用多次,hashCode方法必須始終如一地返回同一個(gè)整數(shù)。
如果兩個(gè)對(duì)象根據(jù)equals方法比較是相等的,那么調(diào)用兩個(gè)對(duì)象的hashCode方法必須返回相同的整數(shù)結(jié)果。
如果兩個(gè)對(duì)象根據(jù)equals方法比較是不等的,則hashCode方法不一定得返回不同的整數(shù)。——《effective java》
System.out.println(new Float(0.0).equals(0.0f));System.out.println(new Float(0.0).equals((float) -0.0));
public boolean equals(Object obj) {return (obj instanceof Float)&& (floatToIntBits(((Float)obj).value) ==floatToIntBits(value));}
/*** Returns a representation of the specified floating-point value* according to the IEEE 754 floating-point "single format" bit* layout.** <p>Bit 31 (the bit that is selected by the mask* {@code 0x80000000}) represents the sign of the floating-point* number.* Bits 30-23 (the bits that are selected by the mask* {@code 0x7f800000}) represent the exponent.* Bits 22-0 (the bits that are selected by the mask* {@code 0x007fffff}) represent the significand (sometimes called* the mantissa) of the floating-point number.** <p>If the argument is positive infinity, the result is* {@code 0x7f800000}.** <p>If the argument is negative infinity, the result is* {@code 0xff800000}.** <p>If the argument is NaN, the result is {@code 0x7fc00000}.** <p>In all cases, the result is an integer that, when given to the* {@link #intBitsToFloat(int)} method, will produce a floating-point* value the same as the argument to {@code floatToIntBits}* (except all NaN values are collapsed to a single* "canonical" NaN value).** @param value a floating-point number.* @return the bits that represent the floating-point number.*/public static int floatToIntBits(float value) {int result = floatToRawIntBits(value);// Check for NaN based on values of bit fields, maximum// exponent and nonzero significand.if (((result & FloatConsts.EXP_BIT_MASK) ==FloatConsts.EXP_BIT_MASK) &&(result & FloatConsts.SIGNIF_BIT_MASK) != 0)result = 0x7fc00000;return result;}
當(dāng)浮點(diǎn)運(yùn)算產(chǎn)生一個(gè)非常接近0的負(fù)浮點(diǎn)數(shù)時(shí),會(huì)產(chǎn)生“-0.0”,而這個(gè)浮點(diǎn)數(shù)不能正常表示
System.out.println(Float.floatToIntBits((float) 0.0));System.out.println(Float.floatToIntBits((float) -0.0));System.out.println(Float.floatToRawIntBits(0.0f));System.out.println(Float.floatToRawIntBits((float)-0.0));
0-21474836480-2147483648
最近熱文閱讀:
1、Redis 官方可視化工具,高顏值,功能太強(qiáng)大! 2、不好意思, Maven 該換了! 3、面試官 | Spring Boot 項(xiàng)目如何統(tǒng)一結(jié)果,統(tǒng)一異常,統(tǒng)一日志? 4、基于SpringBoot+MyBatis+Vue的音樂網(wǎng)站 5、聊聊接口優(yōu)化的幾種方法 6、面試官 | Spring Boot 項(xiàng)目如何統(tǒng)一結(jié)果,統(tǒng)一異常,統(tǒng)一日志? 7、為什么不建議使用ON DUPLICATE KEY UPDATE? 8、Java8 Stream,過分絲滑! 9、8 種最坑SQL語法,工作中踩過嗎? 10、Java 語言“坑爹” TOP 10 關(guān)注公眾號(hào),你想要的Java都在這里
評(píng)論
圖片
表情
