Java經(jīng)典用法總結
在Java編程中,有些知識并不能僅通過語言規(guī)范或者標準API文檔就能學到的,本文為大家羅列。
一、實現(xiàn)
1、現(xiàn)equals()
class Person { String name; int birthYear; byte[] raw; public boolean equals(Object obj) { if (!obj instanceof Person) return false; Person other = (Person)obj; return name.equals(other.name) && birthYear == other.birthYear && Arrays.equals(raw, other.raw); } public int hashCode() { ... } }
- 參數(shù)必須是Object類型,不能是外圍類。
- foo.equals(null) 必須返回false,不能拋NullPointerException。(注意,null instanceof 任意類 總是返回false,因此上面的代碼可以運行。)
- 基本類型域(比如,int)的比較使用 == ,基本類型數(shù)組域的比較使用Arrays.equals()。
- 覆蓋equals()時,記得要相應地覆蓋 hashCode(),與 equals() 保持一致。
2、現(xiàn)hashCode()
class Person { String a; Object b; byte c; int[] d; public int hashCode() { return a.hashCode() + b.hashCode() + c + Arrays.hashCode(d); } public boolean equals(Object o) { ... } }
- 當x和y兩個對象具有x.equals(y) == true ,你必須要確保x.hashCode() == y.hashCode()。
- 根據(jù)逆反命題,如果x.hashCode() != y.hashCode(),那么x.equals(y) == false 必定成立。
- 你不需要保證,當x.equals(y) == false時,x.hashCode() != y.hashCode()。但是,如果你可以盡可能地使它成立的話,這會提高哈希表的性能。
- hashCode()最簡單的合法實現(xiàn)就是簡單地return 0;雖然這個實現(xiàn)是正確的,但是這會導致HashMap這些數(shù)據(jù)結構運行得很慢。
3、實現(xiàn)compareTo()
class Person implements Comparable<Person> { String firstName; String lastName; int birthdate; // Compare by firstName, break ties by lastName, finally break ties by birthdate public int compareTo(Person other) { if (firstName.compareTo(other.firstName) != 0) return firstName.compareTo(other.firstName); else if (lastName.compareTo(other.lastName) != 0) return lastName.compareTo(other.lastName); else if (birthdate < other.birthdate) return -1; else if (birthdate > other.birthdate) return 1; else return 0; } }
總是實現(xiàn)泛型版本 Comparable 而不是實現(xiàn)原始類型 Comparable 。因為這樣可以節(jié)省代碼量和減少不必要的麻煩。
只關心返回結果的正負號(負/零/正),它們的大小不重要。
Comparator.compare()的實現(xiàn)與這個類似。
4、實現(xiàn)clone()
class Values implements Cloneable { String abc; double foo; int[] bars; Date hired; public Values clone() { try { Values result = (Values)super.clone(); result.bars = result.bars.clone(); result.hired = result.hired.clone(); return result; } catch (CloneNotSupportedException e) { // Impossible throw new AssertionError(e); } } }
- 使用 super.clone() 讓Object類負責創(chuàng)建新的對象。
- 基本類型域都已經(jīng)被正確地復制了。同樣,我們不需要去克隆String和BigInteger等不可變類型。
- 手動對所有的非基本類型域(對象和數(shù)組)進行深度復制(deep copy)。
- 實現(xiàn)了Cloneable的類,clone()方法永遠不要拋CloneNotSupportedException。因此,需要捕獲這個異常并忽略它,或者使用不受檢異常(unchecked exception)包裝它。
- 不使用Object.clone()方法而是手動地實現(xiàn)clone()方法是可以的也是合法的。
二、預防性檢測
1、預防性檢測(Defensive checking)數(shù)值
int factorial(int n) { if (n < 0) throw new IllegalArgumentException("Undefined"); else if (n >= 13) throw new ArithmeticException("Result overflow"); else if (n == 0) return 1; else return n * factorial(n - 1); }
- 不要認為輸入的數(shù)值都是正數(shù)、足夠小的數(shù)等等。要顯式地檢測這些條件。
- 一個設計良好的函數(shù)應該對所有可能性的輸入值都能夠正確地執(zhí)行。要確保所有的情況都考慮到了并且不會產(chǎn)生錯誤的輸出(比如溢出)。
2、預防性檢測對象
int findIndex(List<String> list, String target) { if (list == null || target == null) throw new NullPointerException(); ... }
- 不要認為對象參數(shù)不會為空(null)。要顯式地檢測這個條件。
3、預防性檢測數(shù)組索引
void frob(byte[] b, int index) { if (b == null) throw new NullPointerException(); if (index < 0 || index >= b.length) throw new IndexOutOfBoundsException(); ... }
不要認為所以給的數(shù)組索引不會越界。要顯式地檢測它。
4、預防性檢測數(shù)組區(qū)間
void frob(byte[] b, int off, int len) { if (b == null) throw new NullPointerException(); if (off < 0 || off > b.length || len < 0 || b.length - off < len) throw new IndexOutOfBoundsException(); ... }
不要認為所給的數(shù)組區(qū)間(比如,從off開始,讀取len個元素)是不會越界。要顯式地檢測它。
三、數(shù)組
1、填充數(shù)組元素
使用循環(huán):
// Fill each element of array 'a' with 123 byte[] a = (...); for (int i = 0; i < a.length; i++) a[i] = 123; (優(yōu)先)使用標準庫的方法: Arrays.fill(a, (byte)123);
2、復制一個范圍內的數(shù)組元素
使用循環(huán):
// Copy 8 elements from array 'a' starting at offset 3 // to array 'b' starting at offset 6, // assuming 'a' and 'b' are distinct arrays byte[] a = (...); byte[] b = (...); for (int i = 0; i < 8; i++) b[6 + i] = a[3 + i]; (優(yōu)先)使用標準庫的方法: System.arraycopy(a, 3, b, 6, 8);
3、調整數(shù)組大小
使用循環(huán)(擴大規(guī)模):
// Make array 'a' larger to newLen byte[] a = (...); byte[] b = new byte[newLen]; for (int i = 0; i < a.length; i++) // Goes up to length of A b[i] = a[i]; a = b;
使用循環(huán)(減小規(guī)模):
// Make array 'a' smaller to newLen byte[] a = (...); byte[] b = new byte[newLen]; for (int i = 0; i < b.length; i++) // Goes up to length of B b[i] = a[i]; a = b;
(優(yōu)先)使用標準庫的方法:
1a = Arrays.copyOf(a, newLen);
4、把4個字節(jié)包裝(packing)成一個int
int packBigEndian(byte[] b) { return (b[0] & 0xFF) << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF) << 0; } int packLittleEndian(byte[] b) { return (b[0] & 0xFF) << 0 | (b[1] & 0xFF) << 8 | (b[2] & 0xFF) << 16 | (b[3] & 0xFF) << 24; }
5、把int分解(Unpacking)成4個字節(jié)
byte[] unpackBigEndian(int x) { return new byte[] { (byte)(x >>> 24), (byte)(x >>> 16), (byte)(x >>> 8), (byte)(x >>> 0) }; } byte[] unpackLittleEndian(int x) { return new byte[] { (byte)(x >>> 0), (byte)(x >>> 8), (byte)(x >>> 16), (byte)(x >>> 24) }; }
總是使用無符號右移操作符(>>>)對位進行包裝(packing),不要使用算術右移操作符(>>)。
以上就是本文的全部內容,希望對大家的學習有所幫助。
相關文章
JAVA通過HttpClient發(fā)送HTTP請求的方法示例
本篇文章主要介紹了JAVA通過HttpClient發(fā)送HTTP請求的方法示例,詳細的介紹了HttpClient使用,具有一定的參考價值,有興趣的可以了解一下2017-09-09Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮的方法
這篇文章主要介紹了Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮,定義GzipFilter對輸出進行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內容,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10Mybatis Select Count(*)的返回值類型介紹
這篇文章主要介紹了Mybatis Select Count(*)的返回值類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12