当前位置:网站首页>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
边栏推荐
猜你喜欢

Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?

Go微服务(二)——Protobuf详细入门

Bi skills - permission axis

There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks

如何使用Async-Awati异步任务处理代替BackgroundWorker?

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?

Nebula importer data import practice

Build your own website (15)
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
随机推荐
How test engineers "attack the city" (Part I)
redis分布式锁的8大坑总结梳理
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
HDU 1097 A hard puzzle
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
反射(一)
An example of multi module collaboration based on NCF
Summary and sorting of 8 pits of redis distributed lock
Shell 编程核心技术《三》
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
Oracle with as ORA-00903: invalid table name 多表报错
Bi skills - permission axis
Shell 编程核心技术《二》
安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
如何使用Async-Awati异步任務處理代替BackgroundWorker?
LeetCode第300场周赛(20220703)
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
Online text line fixed length fill tool
The 300th weekly match of leetcode (20220703)