欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java數(shù)組轉(zhuǎn)List及Stream的基本方法使用方法

 更新時間:2024年08月20日 12:26:59   作者:ThatMonth  
Java?的?Stream?流操作是一種簡潔而強大的處理集合數(shù)據(jù)的方式,允許對數(shù)據(jù)進行高效的操作,如過濾、映射、排序和聚合,這篇文章主要介紹了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 環(huán)境搭建與基本使用,本文主要是對于Spring Boot Admin的基本認識和基本運用,通過本篇博客能夠?qū)pring Boot Admin有一個宏觀認知和能夠快速上手,需要的朋友可以參考下
    2023-08-08
  • 利用Java工具類Hutool實現(xiàn)驗證碼校驗功能

    利用Java工具類Hutool實現(xiàn)驗證碼校驗功能

    這篇文章主要介紹了利用Java工具類Hutool實現(xiàn)驗證碼校驗功能,利用Hutool實現(xiàn)驗證碼校驗,校驗的Servlet與今天的第一篇是一樣的,唯一就是驗證碼的生成是不一樣的,利用Hutool生成驗證碼更快捷.需要的朋友可以參考下
    2022-10-10
  • JavaFX 監(jiān)聽窗口關(guān)閉事件實例詳解

    JavaFX 監(jiān)聽窗口關(guān)閉事件實例詳解

    這篇文章主要介紹了JavaFX 監(jiān)聽窗口關(guān)閉事件實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • springmvc實現(xiàn)跨服務(wù)器文件上傳功能

    springmvc實現(xiàn)跨服務(wù)器文件上傳功能

    這篇文章主要為大家詳細介紹了springmvc實現(xiàn)跨服務(wù)器文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 詳解Java中Optional類的使用方法

    詳解Java中Optional類的使用方法

    Optional的作用是什么?他都有哪些方法?阿里規(guī)范點名說盡量用Optional來避免空指針,那么什么場景用Optional?本篇文章圍繞這三點來進行講解,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • Java?MyBatis本地緩存原理詳解

    Java?MyBatis本地緩存原理詳解

    這篇文章主要介紹了Java?MyBatis本地緩存原理詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • 一文帶你搞懂Java中的數(shù)據(jù)流處理

    一文帶你搞懂Java中的數(shù)據(jù)流處理

    這篇文章主要為大家詳細介紹了Java中數(shù)據(jù)流處理的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • springboot單元測試依賴踩坑記錄

    springboot單元測試依賴踩坑記錄

    這篇文章主要介紹了springboot單元測試依賴踩坑記錄及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java處理csv文件上傳示例詳解

    java處理csv文件上傳示例詳解

    這篇文章主要為大家詳細介紹了java處理csv文件上傳示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Java程序執(zhí)行過程及內(nèi)存機制詳解

    Java程序執(zhí)行過程及內(nèi)存機制詳解

    本講將介紹Java代碼是如何一步步運行起來的,還會介紹Java程序所占用的內(nèi)存是被如何管理的:堆、棧和方法區(qū)都各自負責(zé)存儲哪些內(nèi)容,感興趣的朋友跟隨小編一起看看吧
    2020-12-12

最新評論