Java數(shù)組轉(zhuǎn)List及Stream的基本方法使用方法
Stream流
Java 的 Stream 流操作是一種簡(jiǎn)潔而強(qiáng)大的處理集合數(shù)據(jù)的方式,允許對(duì)數(shù)據(jù)進(jìn)行高效的操作,如過(guò)濾、映射、排序和聚合。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:過(guò)濾符合條件的元素。
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:跳過(guò)流中的前 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);
//條件過(guò)濾
List<Integer> collect = numList.stream().filter(e -> e > 3).collect(Collectors.toList());
System.out.println("filter 條件過(guò)濾大于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);
//分頁(yè)
List<Integer> collect3 = numList.stream().skip(2).limit(3).collect(Collectors.toList());
System.out.println("skip,limit 分頁(yè)查詢從第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)文章
Spring Boot Admin 環(huán)境搭建與基本使用詳解
這篇文章主要介紹了Spring Boot Admin 環(huán)境搭建與基本使用,本文主要是對(duì)于Spring Boot Admin的基本認(rèn)識(shí)和基本運(yùn)用,通過(guò)本篇博客能夠?qū)pring Boot Admin有一個(gè)宏觀認(rèn)知和能夠快速上手,需要的朋友可以參考下2023-08-08
利用Java工具類(lèi)Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能
這篇文章主要介紹了利用Java工具類(lèi)Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能,利用Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn),校驗(yàn)的Servlet與今天的第一篇是一樣的,唯一就是驗(yàn)證碼的生成是不一樣的,利用Hutool生成驗(yàn)證碼更快捷.需要的朋友可以參考下2022-10-10
JavaFX 監(jiān)聽(tīng)窗口關(guān)閉事件實(shí)例詳解
這篇文章主要介紹了JavaFX 監(jiān)聽(tīng)窗口關(guān)閉事件實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
springmvc實(shí)現(xiàn)跨服務(wù)器文件上傳功能
這篇文章主要為大家詳細(xì)介紹了springmvc實(shí)現(xiàn)跨服務(wù)器文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Java程序執(zhí)行過(guò)程及內(nèi)存機(jī)制詳解
本講將介紹Java代碼是如何一步步運(yùn)行起來(lái)的,還會(huì)介紹Java程序所占用的內(nèi)存是被如何管理的:堆、棧和方法區(qū)都各自負(fù)責(zé)存儲(chǔ)哪些內(nèi)容,感興趣的朋友跟隨小編一起看看吧2020-12-12

