当前位置:网站首页>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
边栏推荐
- Shell programming core technology "four"
- [uniapp] uniapp development app online Preview PDF file
- The 300th weekly match of leetcode (20220703)
- IBM WebSphere MQ retrieving messages
- 牛客小白月赛7 E Applese的超能力
- 1002. A+B for Polynomials (25)(PAT甲级)
- Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
- 安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
- 牛客小白月赛7 I 新建 Microsoft Office Word 文档
- Go microservice (II) - detailed introduction to protobuf
猜你喜欢

Lm10 cosine wave homeopathic grid strategy

每日一题(2022-07-02)——最低加油次数

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

勾股数规律(任意三个数能够满足勾股定理需要满足的条件)

Hough transform Hough transform principle

【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案

“只跑一趟”,小区装维任务主动推荐探索

联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file

Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
随机推荐
Shell 编程核心技术《二》
Hough transform Hough transform principle
26. Delete the duplicate item C solution in the ordered array
指定输出的字符集
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
FPGA时序约束分享01_四大步骤简述
Shell 编程核心技术《四》
神经网络物联网平台搭建(物联网平台搭建实战教程)
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
整理混乱的头文件,我用include what you use
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
Stream流
如何使用Async-Awati异步任務處理代替BackgroundWorker?
明明的随机数
函数式接口
How to use async Awati asynchronous task processing instead of backgroundworker?
Pytorch学习(四)
2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
Nebula importer data import practice