日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

如何在Java中将数组转换为列表

發(fā)布時(shí)間:2023/12/3 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在Java中将数组转换为列表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

你好朋友,

在本教程中,我們將學(xué)習(xí)將數(shù)組轉(zhuǎn)換為L(zhǎng)ist的各種方法。

package com.blogspot.javasolutionsguide;import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils;import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream;public class ArrayToList {public static void main(String[] args) {//Before Java 8System.out.println("Before JAVA 8........................................");// 1.String[] names = new String[] {"Gaurav", "Sachin", "Yuvraj"};List<String> nameList = Arrays.asList(names);System.out.println("using Arrays.asList: " + nameList);//2List<Integer> list = Arrays.asList(1, 2, 3);System.out.println("Inline primitive array to Integer List using Arrays.asList(): "+ list);//Java 8System.out.println("JAVA 8 ................................................");// 3. primitive arrayint intArray[] = {1, 2, 3};List<Integer> integerList1 = Arrays.stream(intArray).boxed().collect(Collectors.toList());System.out.println("Primitive int array to Integer List using Array.stream(): "+ integerList1);// 4. primitive array with IntStreamList<Integer> integerList2 = IntStream.of(intArray).boxed().collect(Collectors.toList());System.out.println("Primitive int array to Integer List using IntStream.of(): "+ integerList2);// 5. Object type array to Integer ListInteger integerArray[] = {1, 2, 3};List<Integer> integerList3 = Arrays.stream(integerArray).collect(Collectors.toList());System.out.println("Object type array to Integer List using Arrays.stream: "+ integerList3);// 6. Integer Array to ArrayListArrayList<Integer> arrayList = Arrays.stream(integerArray).collect(Collectors.toCollection(ArrayList::new));System.out.println("Integer type array to Integer ArrayList using Arrays.stream: "+ arrayList);// 7. Integer Array to LinkedListLinkedList<Integer> linkedList = Stream.of(integerArray).collect(Collectors.toCollection(LinkedList::new));System.out.println("Integer type array to Integer LinkedList using Arrays.stream: "+ linkedList);// 8. Integer Array to Immutable ListList<Integer> immutableList = Collections.unmodifiableList(Arrays.asList(integerArray));System.out.println("Integer type array to Immutable List using Arrays.stream: "+ immutableList);//9. JAVA 9System.out.println("JAVA 9.........................................................");String[] playerNames = {"Sachin", "Dhoni", "Yuvraj"};List<String> players = List.of(playerNames);System.out.println("Array to List using Java 9 List.of() :" + players);//10. JAVA 10System.out.println("JAVA 10.........................................................");List<Integer> integerList = List.copyOf(Arrays.asList(integerArray));System.out.println("Array to List using Java 10 List.copyOf() :" + integerList);//11. Apache CommonsSystem.out.println("Apache Commons...................................................");List<Integer> targetList = new ArrayList<>(6);CollectionUtils.addAll(targetList, integerArray);System.out.println("Array to List using Apache Common CollectionUtils.addAll() :"+ integerList);//12 Google GuavaSystem.out.println("Google Guava......................................................");Integer[] sourceArray = {1, 2, 3};List<Integer> targetList1 = Lists.newArrayList(sourceArray);System.out.println("Array to List using Google Guava Lists.newArrayList:"+ integerList);} }External Dependency Used :<dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId<version>4.4</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.10</version></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>16.0.1</version></dependency>

本教程的所有代碼都可以在GitHub中找到 。 因此在本教程中,我們學(xué)習(xí)了將數(shù)組轉(zhuǎn)換為Java中的List的各種方法,這是本教程的全部?jī)?nèi)容。請(qǐng)訂閱博客以獲取更多此類教程。

翻譯自: https://www.javacodegeeks.com/2020/05/how-to-convert-array-to-list-in-java.html

總結(jié)

以上是生活随笔為你收集整理的如何在Java中将数组转换为列表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。