java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
這里使用java8的stream來進行轉(zhuǎn)化,詳細步驟如下所示:
//初始化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);
上面是分解步驟,實際應(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.
自動裝箱(拆箱也是)是 Java 編譯器提供的原始類型和它的包裝類之間轉(zhuǎn)化的功能。
注意,自動裝箱和拆箱是 Java 編譯器的功能,并不是運行時的。
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() 兩個方法。
Java Integer、int 與 new Integer()
所有整型包裝類對象之間的比較全部使用 equals 方法比較。
對于 Integer var = ? 在 -128 至 127 范圍內(nèi)的賦值,Integer 對象是在 IntegerCache.cache 產(chǎn)生,會復(fù)用已有對象,這個區(qū)間內(nèi)的 Integer 值可以直接使用 == 進行判斷,但是這個區(qū)間之外的所有數(shù)據(jù),都會在堆上產(chǎn)生,并不會復(fù)用已有對象,所以推薦使用 equals 方法進行判斷。
- int 和 Integer 在進行比較的時候,Integer 會進行拆箱,轉(zhuǎn)為 int 值與 int 進行比較。
- Integer 與 Integer 比較的時候,由于直接賦值的時候會進行自動的裝箱。
IntegerCache 為 Integer 類的緩存類,默認緩存了 -128~127 的 Integer 值,如遇到 [-128,127] 范圍的值需要轉(zhuǎn)換為 Integer 時會直接從 IntegerCache 中獲取,具體如以下源碼:
public static Integer valueOf(int i) { ? ? if (i >= IntegerCache.low && i <= IntegerCache.high) ? ? ? ? return IntegerCache.cache[i + (-IntegerCache.low)]; ? ? return new Integer(i); }
當大于這個范圍的時候,直接 new Integer 來創(chuàng)建 Integer 對象。
new Integer(1) 和 Integer a = 1 ,前者創(chuàng)建對象,存儲在堆中,而后者是從 IntegerCache 中獲取的。那么 Integer a = 128, 直接通過 new Integer(128)創(chuàng)建對象,進行裝箱。
Integer i = new Integer(128); Integer j = 128; // 自動裝箱 i == j; // false // == 比較對象的地址是不是相同 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()
當你需要產(chǎn)生一個整形的包裝類的實例的時候(比如整數(shù)10),有兩種方式:
Integer i = new Integer(10);// 已過時,且標記為待刪除。 Integer i = Integer.valueOf(10);
當你用第一種方式時每次都會產(chǎn)生一個新的實例,而當你使用靜態(tài)工廠方法時,你產(chǎn)生的數(shù)是 -128 到127時,不會 new 一個新的對象,超過這個范圍時,同樣是 new 一個新的對象。
為什么 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.
最后一句不是說了嗎,有更好的空間和時間性能,你用 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, 當你傳入小于 128 的值時,返回的是內(nèi)置的緩存值,節(jié)省空間和效率。
int 與 integer 相互轉(zhuǎn)換及區(qū)別
java 提供了兩種類型:int 是 java 的原始數(shù)據(jù)類型,Integer 是 java 為 int 提供的封裝類(引用類型)。
1、Integer 默認值是 null,而 int 默認值是 0;
2、聲明為 Integer 的變量需要實例化,而聲明為 int 的變量不需要實例化;
3、Integer 是對象,用一個引用指向這個對象;而 int 是基本類型,直接存儲數(shù)值。
4、Int 類型是放在棧空間的,Integer 是作為對象放在堆空間的。
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 類型。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot引入Redis報Redis?command?timed?out兩種異常情況
這篇文章主要給大家介紹了關(guān)于SpringBoot引入Redis報Redis?command?timed?out兩種異常情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-08-08SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式
這篇文章主要介紹了SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08