当前位置:网站首页>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
边栏推荐
- Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
- Don't just learn Oracle and MySQL!
- Shell programming core technology II
- 爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
- Detailed explanation of the binary processing function threshold() of opencv
- 2022养生展,健康展,北京大健康展,健康产业展11月举办
- 1011 World Cup Betting (20 分)(PAT甲级)
- 生成XML元素
- node_exporter部署
猜你喜欢
千万不要只学 Oracle、MySQL!
LM10丨余弦波动顺势网格策略
Oracle with as ora-00903: invalid table name multi report error
Go microservice (II) - detailed introduction to protobuf
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
[release] a tool for testing WebService and database connection - dbtest v1.0
To sort out messy header files, I use include what you use
牛客小白月赛7 谁是神箭手
BI技巧丨权限轴
随机推荐
Detailed explanation of the binary processing function threshold() of opencv
如何使用Async-Awati异步任務處理代替BackgroundWorker?
启牛开的证券账户安全吗?
OpenCV的二值化处理函数threshold()详解
876. Intermediate node of linked list
一文掌握数仓中auto analyze的使用
国元期货是正规平台吗?在国元期货开户安全吗?
Shell programming core technology "four"
The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
2021 Hefei informatics competition primary school group
Pytest 可视化测试报告之 Allure
26. 删除有序数组中的重复项 C#解答
Shell 編程核心技術《四》
1002. A+B for Polynomials (25)(PAT甲级)
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
Hough Transform 霍夫变换原理
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
SSL证书续费相关问题详解
如何使用Async-Awati异步任务处理代替BackgroundWorker?