最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2024年08月20日 12:26:59   作者:ThatMonth  
Java?的?Stream?流操作是一種簡(jiǎn)潔而強(qiáng)大的處理集合數(shù)據(jù)的方式,允許對(duì)數(shù)據(jù)進(jìn)行高效的操作,如過濾、映射、排序和聚合,這篇文章主要介紹了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)文章

  • 解讀JDK1.8?默認(rèn)使用什么垃圾收集器

    解讀JDK1.8?默認(rèn)使用什么垃圾收集器

    這篇文章主要介紹了解讀JDK1.8?默認(rèn)使用什么垃圾收集器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java版本不兼容問題詳細(xì)解決方案步驟

    Java版本不兼容問題詳細(xì)解決方案步驟

    這篇文章主要介紹了Java版本不兼容問題解決的相關(guān)資料,詳細(xì)分析了問題原因,并提供了解決方案,包括統(tǒng)一JDK版本、修改項(xiàng)目配置和清理舊版本殘留等步驟,需要的朋友可以參考下
    2025-05-05
  • 使用Zookeeper實(shí)現(xiàn)分布式鎖

    使用Zookeeper實(shí)現(xiàn)分布式鎖

    這篇文章主要介紹了使用Zookeeper實(shí)現(xiàn)分布式鎖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java實(shí)現(xiàn)Socket服務(wù)端與客戶端雙向通信功能

    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)控

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java高并發(fā)下鎖的優(yōu)化詳解

    Java高并發(fā)下鎖的優(yōu)化詳解

    這篇文章主要介紹了Java高并發(fā)下鎖的優(yōu)化詳解,鎖是最常用的同步方法之一,在高并發(fā)的環(huán)境下,激烈的鎖競(jìng)爭(zhēng)會(huì)導(dǎo)致程序的性能下降,下面是一些關(guān)于鎖的使用建議,可以把這種副作用降到最低,需要的朋友可以參考下
    2024-01-01
  • idea運(yùn)行vue項(xiàng)目設(shè)置自定義瀏覽器方式

    idea運(yùn)行vue項(xiàng)目設(shè)置自定義瀏覽器方式

    這篇文章主要介紹了idea運(yùn)行vue項(xiàng)目設(shè)置自定義瀏覽器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SpringBoot實(shí)現(xiàn)簽到打卡功能的五種方案

    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í)行的按鈕問題及解決

    這篇文章主要介紹了SpringBoot單元測(cè)試沒有執(zhí)行的按鈕問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 連接MQTT時(shí)報(bào)錯(cuò)Connection refused: connect及無權(quán)連接的解決過程

    連接MQTT時(shí)報(bào)錯(cuò)Connection refused: connect及無權(quán)連接的解決過程

    MQTT連接錯(cuò)誤解析,重點(diǎn)排查防火墻配置與mosquitto.conf設(shè)置,確保端口開放與遠(yuǎn)程連接權(quán)限配置正確
    2026-05-05

最新評(píng)論

罗甸县| 长泰县| 建水县| 连州市| 加查县| 阿拉尔市| 铜鼓县| 黔西| 海南省| 连城县| 新乡县| 维西| 深州市| 自治县| 商南县| 青铜峡市| 历史| 米泉市| 玛纳斯县| 肃北| 石楼县| 抚宁县| 黄大仙区| 莎车县| 惠水县| 富锦市| 苍南县| 阿荣旗| 连南| 敦化市| 宁夏| 东莞市| 五家渠市| 仪征市| 阜新| 城口县| 射阳县| 湄潭县| 宁德市| 定远县| 犍为县|