首页 > 其他分享 >Stream

Stream

时间:2024-01-30 21:27:51浏览次数:23  
标签:java Stream stream -- List util

Array

int[] anArray = new int[10];
String[] stringArray = new String[] {"a", "b", "c" };  

List

List<String> stringList = java.util.Arrays.asList("a", "b", "c");  
List<String> stringList = java.util.Arrays.asList(array);  

Array --> List

List<String> list = java.util.Arrays.asList(array);  

List --> Array

String[] xx = list.toArray(new String[0]);  

Array --> Stream

Stream<String> stream = java.util.Arrays.stream(array);  

Stream --> Array

String[] array = stream.toArray(String[]::new);  

List --> Stream

Stream<String> stream = list.stream();  

Stream --> List

List<String> list = stream.collect(Collectors.toList());  
List<String> list = stream.toList();    // >= java 16  

Set --> Stream

Stream<String> stream = set.stream();

Stream --> Set

Set<String> set = stream.collect(toSet());

Iterate stream

stream.forEach(System.out::println);  

Create Stream

import java.nio.charset.StandardCharsets;  
import java.nio.file.Files;  
import java.nio.file.Path;  
import java.nio.file.Paths;  
import java.util.*;  
import java.util.regex.Pattern;  
import java.util.stream.*;  
  
Stream<String> stream = Stream.of("a");    >= java 1.9  
Stream<String> stream = List.of("a", "b", "c");     >= java 1.9  
Stream<String> stream = Stream.of(array);    >= java 1.9  
Stream<String> stream = Stream.<String>builder().add("a").add("b").build();  
Stream<String> stream = Stream.generate(() -> "element").limit(8);  
Stream<Integer> stream = Stream.iterate(40, n -> n + 2).limit(20);  
IntStream stream  = IntStream.range(1, 3);  
LongStream stream = LongStream.rangeClosed(1, 3);  
DoubleStream stream =  DoubleStream.of(1.0, 2.0);  
Stream<String> stream = Pattern.compile(", ").splitAsStream("a, b, c");  
  
Path path = Paths.get("c:\\file.txt");  
Stream<String> stream = Files.lines(path);  
Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8);  

Stream --> XXXStream

IntStream stream = Stream.of(1, 2, 3).mapToInt(Integer::intValue);  

Stream --> parallel stream

Stream<String> stream = java.util.Arrays.asList("A", "B", "C", "D", "E").parallelStream();  
IntStream stream = java.util.stream.IntStream.rangeClosed(1, 10).parallel();  

Idiom

String string = Stream.of("apple", "take", "quiver")  
        .map(String::toUpperCase)        
        .collect(Collectors.joining(", ", "[", "]"));    
// output:  ["APPLE", "TAKE", "QUIVER"]  
  
private static Stream<String> streamOf(List<String> list){  
    return list == null || list.isEmpty() ? Stream.empty() : list.stream();
}  

标签:java,Stream,stream,--,List,util
From: https://www.cnblogs.com/alfredxing/p/17997989/stream

相关文章

  • C# 文件流:Stream篇(一)
    前话:本文系列本着备忘的目的进行归纳,Stream系列原文链接:C#温故而知新:Stream篇(—)-逆时针の风-博客园(cnblogs.com) 望各位看官到原作者处学习。后几篇不作注释,还请见谅--------------------------------------------------------------------------------------------分......
  • Apache Paimon:Streaming Lakehouse is Coming
    摘要:本文整理自阿里云智能开源表存储负责人,FounderofPaimon,FlinkPMC成员李劲松(花名:之信)、同程旅行大数据专家,ApacheHudi&PaimonContributor吴祥平、汽车之家大数据计算平台负责人邸星星、联通数科大数据高级技术专家,ApachePaimonContributor王云朋在FlinkForwardAsi......
  • 大语言模型(LLM)运行报错:AttributeError: module 'streamlit' has no attribute 'cache_
    解决方法:......
  • StreamPark从零快速入门(本地调试、功能演示及源码分析)
    本文目录结构:1.引言2.StreamPark项目导入与调试|____Step1:物料准备|____Step2:导入项目|____Step3:配置与打包|____Step4:启动与调试3.演示(新建作业并上线)|____Step1:下载Flink安装包并启动集群|____Step2:配置Flink插件及集群|____Step3:配置作业并上线4.源码分......
  • 收集Stream流的数据到集合或数组中
    1publicstaticvoidmain(String[]args){2List<String>list=newArrayList<>();3list.add("张三");4list.add("李四");5list.add("王武");6list.add("王武"......
  • Stream流操作示例
    1privatestaticdoubleoneMoney;2privatestaticdoubletwoMoney;3privatestaticdoublesumMoney;45publicstaticvoidmain(String[]args){6List<Employee>list1=newArrayList<>();7list1.add(new......
  • Stream流
    1publicstaticvoidmain(String[]args){2//Collection集合获取流3Collection<String>collection=newArrayList<>();4Stream<String>stream=collection.stream();56//Map集合获取流7M......
  • 基于Redis的Stream类型的完美消息队列解决方案(全)
    1概述2追加新消息,XADD,生产消息3从消息队列中获取消息,XREAD,消费消息4消息ID说明5消费者组模式,consumergroup6Pending等待列表7消息转移8坏消息问题,DeadLetter,死信问题9信息监控,XINFO10命令一览11Stream数据结构,RadixTree,基数树12相关产品1概述Redis5.......
  • 学习Java8中StreamAPI的笔记
    本次笔记记录一下我自己学习Stream流的一个情况。第一种:使用Stream流来代替增强for循环进行赋值:这是使用增强for循环的写法:publicstaticvoidmain(String[]args){ArrayList<String>strings=newArrayList<>();strings.add("张三");strings.add("李四");strings.add......
  • JAVA XStream简单使用
    XStreamxStream=newXStream(newStaxDriver());xStream.addPermission(AnyTypePermission.ANY);//不加后面转实体变异常xStream.processAnnotations(resultDTO.getClass());resultDTO=(ResultDTO)xStream.fromXML(result); 1、实体类设置别名  @XStreamAlias("Ma......