当前位置:网站首页>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
边栏推荐
- Summary and sorting of 8 pits of redis distributed lock
- 2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
- [release] a tool for testing WebService and database connection - dbtest v1.0
- 2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- 2021 Hefei informatics competition primary school group
- Go microservice (II) - detailed introduction to protobuf
- SSL证书续费相关问题详解
- Is it safe to open an account at Great Wall Securities? How to open an account when buying stocks
- Is the securities account opened by qiniu safe?
猜你喜欢

LeetCode第300场周赛(20220703)

自由小兵儿
关于判断点是否位于轮廓内的一点思考
![[release] a tool for testing WebService and database connection - dbtest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[release] a tool for testing WebService and database connection - dbtest v1.0

Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples

建立自己的网站(15)

Opencv functions and methods related to binary threshold processing are summarized for comparison and use

英特尔集成光电研究最新进展推动共封装光学和光互连技术进步

Master the use of auto analyze in data warehouse

Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
随机推荐
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
测试工程师如何“攻城”(上)
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
性能优化之关键渲染路径
Oracle with as ora-00903: invalid table name multi report error
1009 Product of Polynomials(25 分)(PAT甲级)
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
876. Intermediate node of linked list
一文掌握数仓中auto analyze的使用
问下各位大佬有用过cdc直接mysql to clickhouse的么
1002. A+B for Polynomials (25)(PAT甲级)
Unity editor extends C to traverse all pictures in folders and subdirectories
Using SSH
Online sql to excel (xls/xlsx) tool
26. Delete the duplicate item C solution in the ordered array
Lm10 cosine wave homeopathic grid strategy
从实时应用角度谈通信总线仲裁机制和网络流控
Shell programming core technology "four"
Shell 编程核心技术《二》