1、迭代entrySet() 獲取Map 的key 和value
當循環中只需要獲取Map 的主鍵key時,迭代keySet() 是正確的;但是,當需要主鍵key 和取值value 時,迭代entrySet() 纔是更高效的做法,其比先迭代keySet() 後再去通過get 取值性能更佳。
優化前
HashMap< String, String> map = new HashMap < > ( ) ;
for ( String key : map. keySet ( ) ) {
String value = map. get ( key) ;
}
優化後
HashMap< String, String> map = new HashMap < > ( ) ;
for ( Map. Entry< String, String> entry: map. entrySet ( ) ) {
String key = entry. getKey ( ) ;
String value = entry. getValue ( ) ;
}
2、使用Collection.isEmpty() 檢測空
使用Collection.size() 來檢測是否爲空在邏輯上沒有問題,但是使用Collection.isEmpty() 使得代碼更易讀,並且可以獲得更好的性能;除此之外,任何Collection.isEmpty() 實現的時間複雜度都是O(1) ,不需要多次循環遍歷,但是某些通過Collection.size() 方法實現的時間複雜度可能是O(n)
優化前
LinkedList<Object> collection=new LinkedList<>();
if (collection.size()==0){
System.out.println("collection is empty");
}
優化後
LinkedList< Object> collection= new LinkedList < > ( ) ;
if ( collection. isEmpty ( ) ) {
System. out. println ( "collection is empty" ) ;
}
if ( CollectionUtils. isEmpty ( collection) ) {
System. out. println ( "collection is empty" ) ;
}
3、初始化集合時儘量指定其大小
儘量在初始化時指定集合的大小,能有效減少集合的擴容次數,因爲集合每次擴容的時間複雜度很可能時O(n),耗費時間和性能。
優化前
int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 } ;
List< Integer> list = new ArrayList < > ( ) ;
for ( int i : arr) {
list. add ( i) ;
}
優化後
int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 } ;
List< Integer> list = new ArrayList < > ( arr. length) ;
for ( int i : arr) {
list. add ( i) ;
}
4、使用StringBuilder 拼接字符串
一般的字符串拼接在編譯期Java 會對其進行優化,但是在循環中字符串的拼接Java 編譯期無法執行優化,所以需要使用StringBuilder 進行替換。
優化前
String str = "" ;
for ( int i = 0 ; i < 10 ; i++ ) {
str += i;
}
優化後
String str1 = "love" ;
String str2 = "you" ;
String str = str1 + str2;
StringBuilder sb = new StringBuilder ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
sb. append ( i) ;
}
5、使用靜態代碼塊實現賦值靜態成員變量
對於集合類型的靜態成員變量,應該使用靜態代碼塊賦值,而不是使用集合實現來賦值。
優化前
private static Map< String, String> map= new HashMap < String, String> ( ) {
{
map. put ( "abc" , "1024" ) ;
map. put ( "adc" , "1021" ) ;
map. put ( "acd" , "1022" ) ;
}
} ;
private static List< Integer> list= new ArrayList < Integer> ( ) {
{
list. add ( 1 ) ;
list. add ( 3 ) ;
list. add ( 2 ) ;
}
} ;
優化後
private static Map< String, String> map = new HashMap < String, String> ( ) ;
static {
{
map. put ( "abc" , "1024" ) ;
map. put ( "adc" , "1021" ) ;
map. put ( "acd" , "1022" ) ;
}
}
;
private static List< Integer> list = new ArrayList < Integer> ( ) ;
static {
{
list. add ( 1 ) ;
list. add ( 3 ) ;
list. add ( 2 ) ;
}
}
;
6、字符串轉化使用String.valueOf(value) 代替 " " + value
把其它對象或類型轉化爲字符串時,使用String.valueOf(value) 比 ""+value 的效率更高。
優化前
int num= 1024 ;
String love= "" + num;
優化後
int num= 1024 ;
String love= String. valueOf ( num) ;
7、 避免使用BigDecimal(double)
BigDecimal(double) 存在精度損失風險,在精確計算或值比較的場景中可能會導致業務邏輯異常。
優化前
BigDecimal bigDecimal= new BigDecimal ( 0.1D ) ;
優化後
BigDecimal bigDecimal= BigDecimal. valueOf ( 0.1D ) ;
8、返回空數組和集合而非 null
若程序運行返回null,需要調用方強制檢測null,否則就會拋出空指針異常;返回空數組或空集合,有效地避免了調用方因爲未檢測null 而拋出空指針異常的情況,還可以刪除調用方檢測null 的語句使代碼更簡潔。
優化前
private static List< Integer> getList ( ) {
return null;
}
private static Map< String, String> getMap ( ) {
return null;
}
優化後
private static List< Integer> getList ( ) {
return Collections. EMPTY_LIST;
}
private static Map< String, String> getMap ( ) {
return Collections. EMPTY_MAP;
}
9、優先使用常量或確定值調用equals 方法
對象的equals 方法容易拋空指針異常,應使用常量或確定有值的對象來調用equals 方法。
優化前
private static boolean get ( String name) {
return name. equals ( "zhang san" ) ;
}
優化後
private static boolean get ( String name) {
return "zhang san" . equals ( name) ;
}
10、String.split(String regex)部分關鍵字需要轉譯
使用字符串String 的plit 方法時,傳入的分隔字符串是正則表達式,則部分關鍵字(比如 .[]()| 等)需要轉義。(需要轉義的字符 . $ | ( ) [ { ^ ? * + \\ )
優化前
String str = "aa.bb.cc" ;
String[ ] split = str. split ( "." ) ;
優化後
String str = "aa.bb.cc" ;
String[ ] split = str. split ( "\\." ) ;
總結的不太全,不喜勿噴