Review the basic usage of Stream pipeline Stream map
Simplest requirement: convert every string in the collection to uppercase!
List<String> alpha = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur"); List<String> alphaUpper = new ArrayList<>(); for (String s : alpha) { alphaUpper.add(s.toUpperCase()); } System.out.println(alphaUpper); //[MONKEY, LION, GIRAFFE, List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors. ToList ()); // The method reference used above, //List<String> collect = alpha.stream().map(s -> s.toupercase ()).collect(Collectors. ToList ()); //List<String> collect = alpha.stream().map(s -> s.toupercase ()). System.out.println(collect); //[MONKEY, LION, GIRAFFE, LEMUR]Copy the code
So the map function is used to transform each data element in the pipeline flow.
Handling collection elements that are not strings
The map() function can not only process data, but also convert data types. As follows:
List<Integer> lengths = alpha.stream() .map(String::length) .collect(Collectors.toList()); System.out.println(lengths); / / [6, 4, 7, 5]Copy the code
Stream.of("Monkey", "Lion", "Giraffe", "Lemur")
.mapToInt(String::length)
.forEach(System.out::println);Copy the code
The output is as follows:
June 4 July 5Copy the code
In addition to mapToInt. There are also maoToLong, mapToDouble and so on
Three, a little more complicated: deal with object data format transformations
Again, use the Employee class from the previous section to create 10 objects. Requirements are as follows:
- Increase the age of each Employee by one year
- Replace “M” in gender with “male” and “F” with Female.
Public static void main(String[] args){Employee e1 = new Employee(1,23,"M","Rick","Beethovan"); Employee e2 = new Employee(2,13,"F","Martina","Hengis"); Employee e3 = new Employee(3,43,"M","Ricky","Martin"); Employee e4 = new Employee(4,26,"M","Jon","Lowman"); Employee e5 = new Employee(5,19,"F","Cristine","Maria"); Employee e6 = new Employee(6,15,"M","David","Feezor"); Employee e7 = new Employee(7,68,"F","Melissa","Roy"); Employee e8 = new Employee(8,79,"M","Alex","Gussin"); Employee e9 = new Employee(9,15,"F","Neetu","Singh"); Employee e10 = new Employee(10,45,"M","Naveen","Jain"); List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); /*List<Employee> maped = employees.stream() .map(e -> { e.setAge(e.getAge() + 1); e.setGender(e.getGender().equals("M")?" male":"female"); return e; }).collect(Collectors.toList()); */ List<Employee> maped = employees.stream() .peek(e -> { e.setAge(e.getAge() + 1); e.setGender(e.getGender().equals("M")?" male":"female"); }).collect(Collectors.toList()); System.out.println(maped); }Copy the code
Since the map argument e is the return value, the peek function can be used. The peek function is a special map function that can be used when the function has no return value or when the parameter is the return value.
Four, flatMap
Map can transform data in a pipe flow, but what if there are pipes in a pipe? Namely: how to deal with two-dimensional arrays and two-dimensional collection classes. Implement a simple requirement: print out each letter of the element as a set of two strings “Hello” and “world”. How do we write it if we don’t have Stream? Write a two-layer for loop. The first layer iterates over the string and splits the string into a char array, and the second for loop iterates over the char array.
List<String> words = Arrays.asList("hello", "word");
words.stream()
.map(w -> Arrays.stream(w.split(""))) //[[h,e,l,l,o],[w,o,r,l,d]]
.forEach(System.out::println);Copy the code
Output the print result:
java.util.stream.ReferencePipeline$Head@3551a94
java.util.stream.ReferencePipeline$Head@531be3c5Copy the code
- You can’t do this with the map method, you can’t do this with the Map method. A map can only operate on one-dimensional arrays, arrays within arrays, pipes within pipes, and it can’t handle every element.
- FlatMap can be understood as expanding all the data in several sub-pipes into the parent pipe for processing.
words.stream()
.flatMap(w -> Arrays.stream(w.split(""))) // [h,e,l,l,o,w,o,r,l,d]
.forEach(System.out::println);Copy the code
Output the print result:
h
e
l
l
o
w
o
r
dCopy the code
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series