java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
這里使用java8的stream來(lái)進(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));
反過(guò)來(lá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 來(lái)創(chuàng)建 Integer 對(duì)象。
new Integer(1) 和 Integer a = 1 ,前者創(chuàng)建對(duì)象,存儲(chǔ)在堆中,而后者是從 IntegerCache 中獲取的。那么 Integer a = 128, 直接通過(guò) 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);// 已過(guò)時(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ì)象,超過(guò)這個(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.
最后一句不是說(shuō)了嗎,有更好的空間和時(shí)間性能,你用 new 也沒(méi)事。
@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)文章
Java并發(fā)編程中的CyclicBarrier使用解析
這篇文章主要介紹了Java并發(fā)編程中的CyclicBarrier使用解析,CyclicBarrier從字面意思上來(lái)看,循環(huán)柵欄,這篇文章就來(lái)分析下是到底是如何實(shí)現(xiàn)循環(huán)和柵欄的,需要的朋友可以參考下2023-12-12Java分布式鎖、分布式ID和分布式事務(wù)的實(shí)現(xiàn)方案
在分布式系統(tǒng)中,分布式鎖、分布式ID和分布式事務(wù)是常用的組件,用于解決并發(fā)控制、唯一標(biāo)識(shí)和數(shù)據(jù)一致性的問(wèn)題,本文將介紹Java中常用的分布式鎖、分布式ID和分布式事務(wù)的實(shí)現(xiàn)方案,并通過(guò)具體的示例代碼演示它們的用法和應(yīng)用場(chǎng)景2023-06-06springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案
這篇文章主要介紹了springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01java中javamail收發(fā)郵件實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了java中javamail收發(fā)郵件實(shí)現(xiàn)方法,實(shí)例分析了javamail的使用方法與相關(guān)注意事項(xiàng),非常具有實(shí)用價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02Java SSM框架(Spring+SpringMVC+MyBatis)搭建過(guò)程
最近一段時(shí)間搭建了ssm環(huán)境,并測(cè)試了幾個(gè)小項(xiàng)目,下面小編通過(guò)圖文并茂的形式給大家分享Java SSM框架(Spring+SpringMVC+MyBatis)搭建過(guò)程,需要的朋友參考下吧2017-11-11Java運(yùn)行時(shí)數(shù)據(jù)區(qū)域(內(nèi)存劃分)的深入講解
聽說(shuō)Java運(yùn)行時(shí)環(huán)境的內(nèi)存劃分是挺進(jìn)BAT的必經(jīng)之路,這篇文章主要給大家介紹了關(guān)于Java運(yùn)行時(shí)數(shù)據(jù)區(qū)域(內(nèi)存劃分)的相關(guān)資料,需要的朋友可以參考下2021-06-06