Java數(shù)組轉(zhuǎn)List及Stream的基本方法使用方法
Stream流
Java 的 Stream 流操作是一種簡潔而強大的處理集合數(shù)據(jù)的方式,允許對數(shù)據(jù)進行高效的操作,如過濾、映射、排序和聚合。Stream API 于 Java 8 引入,極大地簡化了對集合(如 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:將每個元素轉(zhuǎn)換為另一種形式。
stream.map(String::toUpperCase);
sorted:排序流中的元素
stream.sorted(); stream.sorted(Comparator.reverseOrder());
distinct:去除重復(fù)元素
stream.distinct();
limit:截取流中的前 n 個元素
stream.limit(3);
skip:跳過流中的前 n 個元素
stream.skip(2);
三、終端操作
forEach:對流中每個元素執(zhí)行操作。
stream.forEach(System.out::println);
collect:將流轉(zhuǎn)換為另一種形式(如 List、Set)
List<String> resultList = stream.collect(Collectors.toList());
count:返回流中元素的個數(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:將流中的元素組合為一個值。
Optional<String> concatenated = stream.reduce((s1, s2) -> s1 + s2);
anyMatch、allMatch、noneMatch:檢測流中的元素是否匹配給定的條件
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Admin 環(huán)境搭建與基本使用詳解
這篇文章主要介紹了Spring Boot Admin 環(huán)境搭建與基本使用,本文主要是對于Spring Boot Admin的基本認識和基本運用,通過本篇博客能夠?qū)pring Boot Admin有一個宏觀認知和能夠快速上手,需要的朋友可以參考下2023-08-08利用Java工具類Hutool實現(xiàn)驗證碼校驗功能
這篇文章主要介紹了利用Java工具類Hutool實現(xiàn)驗證碼校驗功能,利用Hutool實現(xiàn)驗證碼校驗,校驗的Servlet與今天的第一篇是一樣的,唯一就是驗證碼的生成是不一樣的,利用Hutool生成驗證碼更快捷.需要的朋友可以參考下2022-10-10JavaFX 監(jiān)聽窗口關(guān)閉事件實例詳解
這篇文章主要介紹了JavaFX 監(jiān)聽窗口關(guān)閉事件實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05springmvc實現(xiàn)跨服務(wù)器文件上傳功能
這篇文章主要為大家詳細介紹了springmvc實現(xiàn)跨服務(wù)器文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08