Java數(shù)組轉(zhuǎn)List及Stream的基本方法使用方法
Stream流
Java 的 Stream 流操作是一種簡(jiǎn)潔而強(qiáng)大的處理集合數(shù)據(jù)的方式,允許對(duì)數(shù)據(jù)進(jìn)行高效的操作,如過濾、映射、排序和聚合。Stream API 于 Java 8 引入,極大地簡(jiǎn)化了對(duì)集合(如 List、Set)等數(shù)據(jù)的處理。

一、創(chuàng)建 Stream
從集合創(chuàng)建:
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();從數(shù)組創(chuàng)建:
String[] array = {"a", "b", "c"};
Stream<String> stream = Arrays.stream(array);使用 Stream.of 方法:
Stream<String> stream = Stream.of("a", "b", "c");二、中間操作
filter:過濾符合條件的元素。
stream.filter(s -> s.startsWith("a"));map:將每個(gè)元素轉(zhuǎn)換為另一種形式。
stream.map(String::toUpperCase);
sorted:排序流中的元素
stream.sorted(); stream.sorted(Comparator.reverseOrder());
distinct:去除重復(fù)元素
stream.distinct();
limit:截取流中的前 n 個(gè)元素
stream.limit(3);
skip:跳過流中的前 n 個(gè)元素
stream.skip(2);
三、終端操作
forEach:對(duì)流中每個(gè)元素執(zhí)行操作。
stream.forEach(System.out::println);
collect:將流轉(zhuǎn)換為另一種形式(如 List、Set)
List<String> resultList = stream.collect(Collectors.toList());
count:返回流中元素的個(gè)數(shù)
long count = stream.count();
max:最大值
//數(shù)組獲取最大值
int asInt = Arrays.stream(nums).max().getAsInt();
System.out.println("數(shù)組中最大值: "+asInt);
//List獲取最大值
Integer integer = numList.stream().max(Integer::compare).get();
System.out.println("List中最大值:"+integer);reduce:將流中的元素組合為一個(gè)值。
Optional<String> concatenated = stream.reduce((s1, s2) -> s1 + s2);
anyMatch、allMatch、noneMatch:檢測(cè)流中的元素是否匹配給定的條件
boolean anyStartsWithA = stream.anyMatch(s -> s.startsWith("a"));并行流
List<String> list = Arrays.asList("a", "b", "c");
list.parallelStream().forEach(System.out::println);四、數(shù)組轉(zhuǎn)List
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @description:
* @author: ThatMonth
* @create: 2024-08-16 16:57
**/
public class Test49 {
public static void main(String[] args) {
int[] nums = new int[]{2,1,5,6,2,3};
System.out.println("原始數(shù)組: "+Arrays.toString(nums));
//數(shù)組轉(zhuǎn)List
List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
System.out.println("數(shù)組轉(zhuǎn)List: "+numList);
//List轉(zhuǎn)數(shù)組
int[] array = numList.stream().mapToInt(Integer::intValue).toArray();
System.out.println("List轉(zhuǎn)數(shù)組: "+Arrays.toString(array));
}
}五、綜合使用
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @description:
* @author: ThatMonth
* @create: 2024-08-16 16:57
**/
public class Test {
public static void main(String[] args) {
int[] nums = new int[]{2,1,5,6,2,3};
System.out.println("原始數(shù)組: "+Arrays.toString(nums));
//數(shù)組獲取最大值
int asInt = Arrays.stream(nums).max().getAsInt();
System.out.println("數(shù)組中最大值: "+asInt);
//數(shù)組轉(zhuǎn)List
List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
System.out.println("數(shù)組轉(zhuǎn)List: "+numList);
//List獲取最大值
Integer integer = numList.stream().max(Integer::compare).get();
System.out.println("List中最大值:"+integer);
//條件過濾
List<Integer> collect = numList.stream().filter(e -> e > 3).collect(Collectors.toList());
System.out.println("filter 條件過濾大于3的: "+collect);
//map遍歷修改,(flatmap返回是最低一層的數(shù)據(jù)結(jié)構(gòu),如List<List<Student>>返回的是List<Student>)
List<Integer> collect1 = numList.stream().map(e -> ++e).collect(Collectors.toList());
System.out.println("map 遍歷修改加1: "+collect1);
//規(guī)約求和
int reduc = numList.stream().reduce(100, (e1, e2) -> e1 + e2);
System.out.println("reduce 規(guī)約求和: "+reduc);
//自定義排序
List<Integer> collect2 = numList.stream().sorted((e1,e2)->e1-e2).collect(Collectors.toList());
System.out.println("sorted 自定義排序: "+collect2);
//任意匹配
boolean m = numList.stream().anyMatch(e1->e1>100);
System.out.println("anyMatch 是否存在大于100的: "+m);
//分頁
List<Integer> collect3 = numList.stream().skip(2).limit(3).collect(Collectors.toList());
System.out.println("skip,limit 分頁查詢從第2條起,查3條: "+collect3);
//去重
List<Integer> collect4 = numList.stream().distinct().collect(Collectors.toList());
System.out.println("distinct 去重: "+collect4);
//轉(zhuǎn)字符串并用‘--'連接
String collect5 = numList.stream().map(e ->e.toString()).collect(Collectors.joining("--"));
System.out.println("Collectors.joining 轉(zhuǎn)字符串并用‘--'連接: "+collect5);
//List轉(zhuǎn)數(shù)組
int[] array = numList.stream().mapToInt(Integer::intValue).toArray();
System.out.println("List轉(zhuǎn)數(shù)組: "+Arrays.toString(array));
}
}示例:

到此這篇關(guān)于Java數(shù)組轉(zhuǎn)List及Stream的基本方法使用教程的文章就介紹到這了,更多相關(guān)Java數(shù)組轉(zhuǎn)List內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)Socket服務(wù)端與客戶端雙向通信功能
大家好,由于工作上業(yè)務(wù)的需要,在java項(xiàng)目中引入了socket通信,特此記錄一下,用以備份,本文章中的socket通信實(shí)現(xiàn)了,服務(wù)端與客戶端的雙向通訊,以及二者之間的心跳通信,服務(wù)端重啟之后,客戶端的自動(dòng)重連功能,需要的朋友可以參考下2025-04-04
SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
idea運(yùn)行vue項(xiàng)目設(shè)置自定義瀏覽器方式
這篇文章主要介紹了idea運(yùn)行vue項(xiàng)目設(shè)置自定義瀏覽器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot實(shí)現(xiàn)簽到打卡功能的五種方案
在現(xiàn)代應(yīng)用開發(fā)中,簽到打卡功能廣泛應(yīng)用于企業(yè)考勤管理、在線教育、社區(qū)運(yùn)營等多個(gè)領(lǐng)域,它不僅是一種記錄用戶行為的方式,也是提升用戶粘性和活躍度的重要手段,本文將介紹5種簽到打卡的實(shí)現(xiàn)方案,需要的朋友可以參考下2025-06-06
SpringBoot單元測(cè)試沒有執(zhí)行的按鈕問題及解決
這篇文章主要介紹了SpringBoot單元測(cè)試沒有執(zhí)行的按鈕問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
連接MQTT時(shí)報(bào)錯(cuò)Connection refused: connect及無權(quán)連接的解決過程
MQTT連接錯(cuò)誤解析,重點(diǎn)排查防火墻配置與mosquitto.conf設(shè)置,確保端口開放與遠(yuǎn)程連接權(quán)限配置正確2026-05-05

