java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
這里使用java8的stream來進(jìn)行轉(zhuǎn)化,詳細(xì)步驟如下所示:
//初始化int數(shù)組 int[] nums = {1,2,3,4,5,6}; //將int數(shù)組轉(zhuǎn)換為數(shù)值流 IntStream stream = Arrays.stream(nums); //流中的元素全部裝箱,轉(zhuǎn)換為Integer流? Stream<Integer> integerStream = stream.boxed(); //將流轉(zhuǎn)換為數(shù)組 Integer[] integers = integerStream.toArray(Integer[]::new);
上面是分解步驟,實(shí)際應(yīng)用中一行代碼即可解決
Integer newNums[] = Arrays.stream(nums).boxed().toArray(Integer[]::new);
Java int和Integer互轉(zhuǎn)原理
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
自動(dòng)裝箱(拆箱也是)是 Java 編譯器提供的原始類型和它的包裝類之間轉(zhuǎn)化的功能。
注意,自動(dòng)裝箱和拆箱是 Java 編譯器的功能,并不是運(yùn)行時(shí)的。
int 轉(zhuǎn) Integer:
List<Integer> li = new ArrayList<>(); for (int i = 1; i < 50; i += 2) ? ? li.add(i); // Java 編譯器把上面的代碼轉(zhuǎn)換成了下面的樣子 List<Integer> li = new ArrayList<>(); for (int i = 1; i < 50; i += 2) ? ? li.add(Integer.valueOf(i));
反過來,Integer 轉(zhuǎn) int:
public static int sumEven(List<Integer> li) { ? ? int sum = 0; ? ? for (Integer i: li) ? ? ? ? if (i % 2 == 0) ? ? ? ? ? ? sum += i; ? ?return sum; } // 編譯器把上面的代碼轉(zhuǎn)化成了下面的樣子 public static int sumEven(List<Integer> li) { ? ? int sum = 0; ? ? for (Integer i : li) ? ? ? ? if (i.intValue() % 2 == 0) ? ? ? ? ? ? sum += i.intValue(); ? ? ? ? return sum; }
Java 中 int 和I nteger 互轉(zhuǎn),原理是 Java 編譯器幫你調(diào)用了包裝類的 valueOf() 和 intValue() 兩個(gè)方法。
Java Integer、int 與 new Integer()
所有整型包裝類對(duì)象之間的比較全部使用 equals 方法比較。
對(duì)于 Integer var = ? 在 -128 至 127 范圍內(nèi)的賦值,Integer 對(duì)象是在 IntegerCache.cache 產(chǎn)生,會(huì)復(fù)用已有對(duì)象,這個(gè)區(qū)間內(nèi)的 Integer 值可以直接使用 == 進(jìn)行判斷,但是這個(gè)區(qū)間之外的所有數(shù)據(jù),都會(huì)在堆上產(chǎn)生,并不會(huì)復(fù)用已有對(duì)象,所以推薦使用 equals 方法進(jìn)行判斷。
- int 和 Integer 在進(jìn)行比較的時(shí)候,Integer 會(huì)進(jìn)行拆箱,轉(zhuǎn)為 int 值與 int 進(jìn)行比較。
- Integer 與 Integer 比較的時(shí)候,由于直接賦值的時(shí)候會(huì)進(jìn)行自動(dòng)的裝箱。
IntegerCache 為 Integer 類的緩存類,默認(rèn)緩存了 -128~127 的 Integer 值,如遇到 [-128,127] 范圍的值需要轉(zhuǎn)換為 Integer 時(shí)會(huì)直接從 IntegerCache 中獲取,具體如以下源碼:
public static Integer valueOf(int i) { ? ? if (i >= IntegerCache.low && i <= IntegerCache.high) ? ? ? ? return IntegerCache.cache[i + (-IntegerCache.low)]; ? ? return new Integer(i); }
當(dāng)大于這個(gè)范圍的時(shí)候,直接 new Integer 來創(chuàng)建 Integer 對(duì)象。
new Integer(1) 和 Integer a = 1 ,前者創(chuàng)建對(duì)象,存儲(chǔ)在堆中,而后者是從 IntegerCache 中獲取的。那么 Integer a = 128, 直接通過 new Integer(128)創(chuàng)建對(duì)象,進(jìn)行裝箱。
Integer i = new Integer(128); Integer j = 128; // 自動(dòng)裝箱 i == j; // false // == 比較對(duì)象的地址是不是相同 i.equals(j); // true Integer i = new Integer(127); Integer j = 127; i == j; // false i.equals(j); // true Integer i = 128; Integer j = 128; i == j; // false i.equals(j); // true Integer i = 127; Integer j = 127; i == j; // true i.equals(j); // true
Integer.valueOf() new Integer()
當(dāng)你需要產(chǎn)生一個(gè)整形的包裝類的實(shí)例的時(shí)候(比如整數(shù)10),有兩種方式:
Integer i = new Integer(10);// 已過時(shí),且標(biāo)記為待刪除。 Integer i = Integer.valueOf(10);
當(dāng)你用第一種方式時(shí)每次都會(huì)產(chǎn)生一個(gè)新的實(shí)例,而當(dāng)你使用靜態(tài)工廠方法時(shí),你產(chǎn)生的數(shù)是 -128 到127時(shí),不會(huì) new 一個(gè)新的對(duì)象,超過這個(gè)范圍時(shí),同樣是 new 一個(gè)新的對(duì)象。
為什么 Java 9 不建議使用 new Integer 了?
java.lang.Integer @Deprecated(since = “9”) @Contract(pure = true)
It is rarely appropriate to use this constructor. The static factory valueOf(int) is generally a better choice, as it is likely to yield significantly better space and time performance.
最后一句不是說了嗎,有更好的空間和時(shí)間性能,你用 new 也沒事。
@HotSpotIntrinsicCandidate ? ? public static Integer valueOf(int i) { ? ? ? ? if (i >= IntegerCache.low && i <= IntegerCache.high) ? ? ? ? ? ? return IntegerCache.cache[i + (-IntegerCache.low)]; ? ? ? ? return new Integer(i); ? ? }
上面是 valueOf, 當(dāng)你傳入小于 128 的值時(shí),返回的是內(nèi)置的緩存值,節(jié)省空間和效率。
int 與 integer 相互轉(zhuǎn)換及區(qū)別
java 提供了兩種類型:int 是 java 的原始數(shù)據(jù)類型,Integer 是 java 為 int 提供的封裝類(引用類型)。
1、Integer 默認(rèn)值是 null,而 int 默認(rèn)值是 0;
2、聲明為 Integer 的變量需要實(shí)例化,而聲明為 int 的變量不需要實(shí)例化;
3、Integer 是對(duì)象,用一個(gè)引用指向這個(gè)對(duì)象;而 int 是基本類型,直接存儲(chǔ)數(shù)值。
4、Int 類型是放在??臻g的,Integer 是作為對(duì)象放在堆空間的。
int 到 Integer:
int a = 66;? // Integer A = new Integer(a); Integer A = Integer.valueOf(a); Integer 到 int: Integer A = Integer.valueOf(66); int a = A.intValue();
String 類型轉(zhuǎn)為 int 類型:
Integer.parseInt(String str) // String 類型轉(zhuǎn)為 int 類型。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況
這篇文章主要給大家介紹了關(guān)于SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式
這篇文章主要介紹了SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08解決java連接虛擬機(jī)Hbase無反應(yīng)的問題
這篇文章主要介紹了解決java連接虛擬機(jī)Hbase無反應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06