当前位置:网站首页>List.stream common operations
List.stream common operations
2022-07-25 02:38:00 【It is not easy to live in vain】
list.stream Common operations
Java8 A series of operations can be carried out in the way of flow in the set , We will directly illustrate through some examples .
So let's define one list, And initialize some data :
List<String> list = new ArrayList() {
{
add(" Xiao Ming ");
add(" Xiaohong ");
add(" Xiao Chen ");
add(" Xiao Zhang ");
add(" Xiao Zhang ");
add(" petty thief ");
add(" Xiao Wang ");
}};
- Find the first element in the collection that meets the criteria
// Find the first object in the collection
Optional<String> first = list.stream().filter(a -> " petty thief ".equals(a)).findFirst();
System.out.println(first); // Output : Optional[ petty thief ]
System.out.println(first.toString()); // Output :Optional[ petty thief ]
- Return the set that meets the query criteria
// Returns a collection that meets the criteria
List<String> collect = list.stream().filter(a -> " Xiao Zhang ".equals(a)).collect(Collectors.toList());
System.out.println(collect); // [ Xiao Zhang , Xiao Zhang ]
System.out.println(collect.toString()); // [ Xiao Zhang , Xiao Zhang ]
1. filter Filter
// filter Filter
List<Integer> list1 = Arrays.asList(21,32,22,33,44);
List<Integer> collect1 = list1.stream().filter(tmp -> tmp > 25).collect(Collectors.toList());
System.out.println(collect1); // [32, 33, 44]
2. map Operate on each element , Will not change the original quantity
List<String> collect2 = list.stream().map(a -> a + " Hello ").collect(Collectors.toList());
System.out.println(collect2); // [ Hello Xiaoming , Hello, Xiaohong , Hello, Xiao Chen , Hello, Xiao Zhang , Hello, Xiao Zhang , Hello, Xiao Li , Hello, Xiao Wang ]
3. mapToInt Turn into IntStream
// mapToInt Turn into IntSream
IntStream intStream = list1.stream().mapToInt(a -> a);
List<Integer> collect3 = intStream.boxed().collect(Collectors.toList());
System.out.println(collect3); // [21, 32, 22, 33, 44]
// mapToDouble mapToLong Empathy
4. flatMap Integrate multiple sets into one
// flatMap Integrate multiple sets into one
List<List<Integer>> lists = new ArrayList() {
{
add(Arrays.asList(1,2,3));
add(Arrays.asList(4,5,6));
}};
System.out.println(lists); // [[1, 2, 3], [4, 5, 6]]
Stream<Integer> integerStream = lists.stream().flatMap(Collection::stream);
List<Integer> collect4 = integerStream.collect(Collectors.toList());
System.out.println(collect4); // [1, 2, 3, 4, 5, 6]
5. distinct duplicate removal
// distinct duplicate removal
List<Integer> list2 = Arrays.asList(1,1,1,2,2,2,3,3,3);
List<Integer> collect5 = list2.stream().distinct().collect(Collectors.toList());
System.out.println(collect5); // [1, 2, 3]
6. sorted Sort
// sort Sort
System.out.println(list1);
List<Integer> collect6 = list1.stream().sorted().collect(Collectors.toList());
System.out.println(collect6); // [21, 22, 32, 33, 44]
7. peek Without affecting the mainstream , Extract the current stream elements
// peek Without affecting the mainstream , Extract the current stream elements
System.out.println(list); // [ Xiao Ming , Xiaohong , Xiao Chen , Xiao Zhang , Xiao Zhang , petty thief , Xiao Wang ]
List<String> nozhang = new ArrayList<>();
List<String> noming = new ArrayList<>();
List<String> nohong = list.stream().filter(a -> !a.contains(" Zhang ")).peek(nozhang :: add)
.filter(a -> !a.contains(" bright ")).peek(noming :: add)
.filter(a -> !a.contains(" red ")).collect(Collectors.toList());
System.out.println(list); // [ Xiao Ming , Xiaohong , Xiao Chen , Xiao Zhang , Xiao Zhang , petty thief , Xiao Wang ]
System.out.println(nozhang); // [ Xiao Ming , Xiaohong , Xiao Chen , petty thief , Xiao Wang ]
System.out.println(noming); // [ Xiaohong , Xiao Chen , petty thief , Xiao Wang ]
System.out.println(nohong); // [ Xiao Chen , petty thief , Xiao Wang ]
8. limit Limit the number of flow elements
// limit Limit the number of flow elements
System.out.println(collect4); // [1, 2, 3, 4, 5, 6]
List<Integer> collect7 = collect4.stream().limit(3).collect(Collectors.toList());
System.out.println(collect7); // [1, 2, 3]
9. skip How many elements to skip
// skip How many elements to skip
System.out.println(collect4); // [1, 2, 3, 4, 5, 6]
List<Integer> collect8 = collect4.stream().skip(2).collect(Collectors.toList());
System.out.println(collect8); // [3, 4, 5, 6]
10. foreach Traverse
// foreach Traverse
System.out.println(collect4); // [1, 2, 3, 4, 5, 6]
List<Integer> foreach = new ArrayList<>();
List<Integer> foreach1 = new ArrayList<>();
collect4.stream().forEach(foreach :: add);
collect4.stream().forEachOrdered(foreach1 :: add);
System.out.println(foreach); // [1, 2, 3, 4, 5, 6]
System.out.println(foreach1); // [1, 2, 3, 4, 5, 6]
11. reduce Calculation
// reduce Calculation
System.out.println(collect4); // [1, 2, 3, 4, 5, 6]
Integer reduce = collect4.stream().reduce(0, (x, y) -> x + y);
System.out.println(reduce); // 21
Integer integer = collect4.stream().reduce((x, y) -> x + y).orElse(-1);
System.out.println(integer); // 21
Integer sum = collect4.stream().reduce(0, (x, y) -> x + y, Integer::sum);
System.out.println(sum); // 21
12. min Minimum max Maximum count total
// min Minimum max Maximum count total
System.out.println(collect4); // [1, 2, 3, 4, 5, 6]
Integer min = collect4.stream().min(Integer::compareTo).orElse(-1);
Integer max = collect4.stream().max(Integer::compareTo).orElse(-1);
long count = collect4.stream().count();
System.out.println(min); // 1
System.out.println(max); // 6
System.out.println(count); // 6
13. findFirst first findAny Any one
// findFirst first findAny Any one
System.out.println(list); // [ Xiao Ming , Xiaohong , Xiao Chen , Xiao Zhang , Xiao Zhang , petty thief , Xiao Wang ]
String s = list.stream().findFirst().toString();
String s1 = list.stream().findAny().orElse("");
System.out.println(s); // Optional[ Xiao Ming ]
System.out.println(s1); // Xiao Ming
边栏推荐
- Introduction to web security telent testing and defense
- After upgrading v2.1.0, the synchronization failed
- A weird jedisconnectionexception: connection rejected problem
- "I gave up programming and wrote a 1.3 million word hard science fiction."
- Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
- Detailed explanation of the principles and differences between static pages and dynamic pages
- Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
- Explorer TSSD 2019 software installation package download and installation tutorial
- In the post deep learning era, where is the recommendation system going?
- Four redis cluster schemes you must know and their advantages and disadvantages
猜你喜欢

Details of C language compilation preprocessing and comparison of macros and functions

My creation anniversary (3rd Anniversary)

Simulate the implementation of StrCmp function

Cookies and sessions

Ctfshow misc introduction

These 11 chrome artifacts are extremely cool to use

R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set

Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming

In the post deep learning era, where is the recommendation system going?
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
随机推荐
Get to know string thoroughly
B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry
After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
"I gave up programming and wrote a 1.3 million word hard science fiction."
"Introduction to interface testing" punch in to learn day09: Micro service interface: how to use mock to solve chaotic call relationships
In the post deep learning era, where is the recommendation system going?
VRRP virtual redundancy protocol configuration
Large number processing -- use case
ICO objects in classification
Summary and sorting of XSS (cross site script attack) related content
How to communicate with aliens
Tp5.1 initialize initialization method (not \u initialize)
Detailed explanation of the principles and differences between static pages and dynamic pages
How to judge which star you look like?
How to upload files to the server
How to use blender to make 360 degree panorama and panoramic video?
After six years of precipitation, new consumption has found a digital transformation paradigm
Ten year structure and five-year Life-03 trouble as a technical team leader
MySQL advanced (13) command line export import database
Creating elements of DOM series