当前位置:网站首页>Stream stream
Stream stream
2022-07-04 19:32:00 【Cold Snowflakes】
Stream How the stream is generated
Collection A collection of systems , Use Collection Default method for interface ,stream Method generate stream
// Collection Collective use of the system Default method for interface stream() Generative flow
List<String> list = new ArrayList<>();
Stream<String> listStream = list.stream();
Set<String> set = new HashSet<>();
Stream<String> setStream = set.stream();
// Map The collection of systems generates streams indirectly
Map<String,Integer> map = new HashMap<>();
Stream<String> keySetStream = map.keySet().stream();
Stream<Integer> valuesStream = map.values().stream();
Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();
// Array pass Stream Static methods of the interface of(T ...values) Generative flow
String[] strArray = {
"hello","world","java"};
Stream<String> strArrayStream = Stream.of(strArray);
Stream<String> strArrayStream2 = Stream.of("hello", "world", "java");
Stream<Integer> integerStream = Stream.of(10, 20, 30);
System.out.println(list.stream().getClass());
System.out.println(new HashSet<>().stream().getClass());
// class java.util.stream.ReferencePipeline$Head
// class java.util.stream.ReferencePipeline$Head
System.out.println(list.getClass());
// class java.util.ArrayList
System.out.println(new HashSet<>().getClass());
// class java.util.HashSet
Stream Common intermediate operations of flow
// The method in ReferencePipeline Class , What we need to do is , Provide Predicate Interface test Implementation logic of abstract methods .
// The rest , The source code has already helped us .
Stream<T> filter(Predicate<? super T> predicate);
ArrayList<String> list = new ArrayList<>();
list.add(" Brigitte Lin ");
list.add(" Maggie Cheung ");
list.add(" Joey wong ");
list.add(" Liu Yan ");
list.add(" Zhang min ");
list.add(" zhang wuji ");
list.stream().filter(s -> s.startsWith(" Zhang "))
.filter(s -> s.length() == 3)
.forEach(System.out::println);
// These two methods are also in ReferencePipeline Class
Stream<T> limit(long maxSize);
Stream<T> skip(long n);
// Take the first two data
list.stream().limit(2).forEach(System.out::println);
System.out.println("------------");
// Skip the first two data
list.stream().skip(2).forEach(System.out::println);
System.out.println("------------");
// Skip the first two data , Take the first two of the remaining elements
list.stream().skip(2).limit(2).forEach(System.out::println);
// Stream Static methods in interfaces , You need to use an interface to call
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
// stay ReferencePipeline Class
Stream<T> distinct()
Stream<String> s1 = list.stream().limit(4);
Stream<String> s2 = list.stream().skip(2);
// Merge two streams
Stream.concat(s1,s2).forEach(System.out::println);
// Returns a stream consisting of different elements of the stream
Stream.concat(s1,s2).distinct().forEach(System.out::println);
// These two methods are also in ReferencePipeline Class
Stream<T> sorted() // Natural order
Stream<T> sorted(Comparator<? super T> comparator) // Sort according to the specified comparator
list.stream().sorted((s1,s2) -> {
int num = s1.length() - s2.length();
int num2 = num == 0 ? s1.compareTo(s2):num;
return num2;
}).forEach(System.out::println);
// These two methods are also in ReferencePipeline Class .
// Parameters : Realization Function Interface apply Methodical lambda expression
<R> Stream<R> map(Function<? super T, ? extends R> mapper); // Set the given function apply Apply to this stream element
// IntStream It means original int flow , ToIntFunction Abstract methods in interfaces :`int applyAsInt(T value);`
IntStream mapToInt(ToIntFunction<? super T> mapper); // Set the given function applyAsInt Apply to this stream element
ArrayList<String> list = new ArrayList<>();
list.add("10");
list.add("20");
list.add("30");
list.add("40");
list.add("50");
list.stream().map(Integer::parseInt).forEach(System.out::println);
int result = list.stream().mapToInt(Integer::parseInt).sum();
System.out.println(result);
Stream Common stream termination operations
void forEach(Consumer<? super T> action); // Perform operations on each element of this flow
long count(); // Returns the number of elements in this stream
ArrayList<String> list = new ArrayList<>();
list.add(" Brigitte Lin ");
list.add(" Maggie Cheung ");
list.add(" Joey wong ");
list.stream().forEach(System.out::print);
long count = list.stream().filter(s -> s.startsWith(" Zhang ")).count();
System.out.println(count);
Stream Stream collection operation
// stay ReferencePipeline Implementation in
<R, A> R collect(Collector<? super T, A, R> collector);
Collectors Class has a static inner class CollectorImpl Realized Collector Interface
static class CollectorImpl<T, A, R> implements Collector<T, A, R>
Here are Collectors Class static methods , Return to one CollectorImpl object .
Collector<T, ?, List<T>> toList()
Collector<T, ?, Set<T>> toSet()
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
List<String> newList = list.stream().filter(s -> s.startsWith(" Zhang ")).collect(Collectors.toList());
System.out.println(newList);
So you are Stream
The difference between an inner class and a static inner class
Java Stream Source code in-depth analysis
边栏推荐
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
- How to use async Awati asynchronous task processing instead of backgroundworker?
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
- 勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
- 生成XML元素
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
- The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
- 测试工程师如何“攻城”(上)
- Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
- Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
猜你喜欢
用实际例子详细探究OpenCV的轮廓绘制函数drawContours()
Master the use of auto analyze in data warehouse
Build your own website (15)
FPGA timing constraint sharing 01_ Brief description of the four steps
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
Stream流
[uniapp] uniapp development app online Preview PDF file
Hough transform Hough transform principle
Wireshark网络抓包
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
随机推荐
Wireshark网络抓包
偏移量函数及开窗函数
Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
node_exporter部署
基于NCF的多模块协同实例
Online text line fixed length fill tool
ftp、sftp文件传输
How test engineers "attack the city" (Part 2)
建立自己的网站(15)
一文掌握数仓中auto analyze的使用
The 300th weekly match of leetcode (20220703)
PolyFit软件介绍
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
FPGA时序约束分享01_四大步骤简述
Jetpack Compose 教程
[release] a tool for testing WebService and database connection - dbtest v1.0
如何使用Async-Awati异步任务处理代替BackgroundWorker?
Build your own website (15)
数组中的第K个最大元素