-Shows typical methods of StreamAPI and basic coding examples. -The idea, application examples, and techniques of StreamAPI will be delegated to other entries, and we will try to write them as simply as possible here. -Stream is generally written by giving a lambda expression or method reference, but I dare to declare a functional interface.
-API for handling a set of data added in SE8. The main purpose is to support parallel processing. Using the Stream API makes it easy to switch from sequential processing to parallel processing.
-The interface under the java.util.stream package, which is different from the stream used in the java.io package.
-The java.util.stream package is based on the BaseStream interface and consists of a Stream interface for handling reference types and three interfaces for handling primitive types.
-Stream is generated based on a data set such as List or Map, and the result is obtained by executing 0 or more intermediate operations and 1 termination operation.
Java (tm) Platform Standard Edition 8 Package java.util.stream Introduction to Java Stream API-Basic Rough summary of Java8 Stream
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
int[] array = { 1, 2, 3, 4, 5 };
IntStream intStream = Arrays.stream(array);
IntStream intStream = IntStream.range(1, 5);
Path path = Paths.get("D:\\hoge.txt");
Stream<String> fileStream = Files.lines(path)
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
stream.forEach(System.out::println);
Generates an Integer type Stream and executes standard output processing with 0 intermediate operations and 1 termination operation. forEach is a termination operation. The termination operation will be described later. Basically, this is combined with any number of different intermediate operations and one of the same various termination operations.
Try to stream text to Java 8 Stream API (generation)
Stream<String> stream = Stream.of("A", "B", "A", "B", "B", "A");
Predicate<String> isA = "A"::equals;
stream.filter(isA).forEach(System.out::println);
An image of thinning out False with a predicate type isHogeHoge in between.
Predicate
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Function<Integer, Integer> toDouble = param -> param * 2;
stream.map(toDouble).forEach(System.out::println);
Returns a Stream with the corresponding value, using each element of the Stream as a key. In short, it's like conversion processing. The sample used Function for clarity, but in this case Unary Operator is smart. The lambda expression is below. Summary of Java8 Lambda extension Until the interface implementation becomes lambda
Stream<Integer> stream = Stream.of(3, 2, 1, 5, 4);
Comparator<Integer> comparator = Comparator.reverseOrder();
stream.sorted(comparator).forEach(System.out::println);
Specify the order by giving a comparator to sorted. The following is about Comparator. (o1, o2)-> o1 --o2 Stop spells! --How to use Comparator in Java 8
Stream<String> stream = Stream.of("George", "John", "Ringo", "Paul");
Predicate<String> isRingo = "Ringo"::equals;
boolean isMatch = stream.anyMatch(isRingo);
There are also variations such as allMatch and noneMatch.
Stream<String> countSt = Stream.of("George", "John", "Ringo", "Paul");
long result = countSt.count();
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
BinaryOperator<Integer> sum = Integer::sum;
Optional<Integer> result = stream.reduce(sum);
Process the result into a single value, such as a convolution. The return value is Optional. The following is for Optional. Java 8 "Optional" -How to deal with null in the future-
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
List<Integer> resultList = stream.collect(Collectors.toCollection(ArrayList::new));
resultList.forEach(System.out::print);
Process into a set such as ArrayList.
I tried to stream the text to Java 8 Stream API (Termination operation) About reduction operation of Java8 Stream
Stream<String> stream= Stream.of("A", "B", "C", "D", "E", "F");
stream.parallel().forEach(param -> System.out.println(param + " thread-id: " + Thread.currentThread().getId()));
Parallel processing can be achieved simply by using pararelStream () at the time of generation or by biting parallel () in an intermediate operation.
Now, let's start parallel programming About the proper use of Stream and Parallel Stream in Java 8 Discussion on stream and parallelStream How different is the performance of each Java writing style? I compared the measurements. Stream and loop
I can't write parallel processing, so I won't go into detail.
Recommended Posts