source .intermidiate operation .intermidiate operation .intermidiate operation .terminal operation
package com.mycompany.sandbox; import java.util.ArrayList; import java.util.List; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor class Person { private String name; private int age; } public class StreamExam { public static void main(String[] args) { List<Person> list = new ArrayList<>(); list.add(new Person("Zach", 18)); list.add(new Person("Albert", 59)); list.add(new Person("Donna", 38)); list.add(new Person("Erich", 40)); list.add(new Person("Brenda", 16)); list.stream() .sorted((a,b) -> a.getName().compareTo(b.getName())) .forEach(p -> System.out.println(p)); } }
Person(name=Albert, age=59) Person(name=Brenda, age=16) Person(name=Donna, age=38) Person(name=Erich, age=40) Person(name=Zach, age=18)
1 2 3
1 2
1 2 3
// Caos Logistic function Stream.iterate(0.1, i -> 3.58 * i * (1.0 -i)).limit(10).forEach(System.out::println);
0.1 0.32220000000000004 0.7818260328000001 0.6106552323056436 0.851164402036915 0.4535271546165602 0.8872681832182414 0.3580836082752965 0.8228978611802291 0.5217383570579359周期が無限大以上になるロジスティック方程式 (カオス)の最初の10個値からなる Stream を作る。通常 iterate() と limit() を組み合わせて 有限の Stream にする。
IntStream.of(1,2,3,3) .distinct() .forEach(System.out::println);
1 2 3
IntStream.of(1,2,3,3) .filter(i -> i > 2) .forEach(System.out::println);
3 3
IntStream.concat(IntStream.range(0,3), IntStream.range(100,103)) .forEach(System.out::println);
0 1 2 100 101 102
IntStream.of(1,2,3,3) .map(i -> i * 2) .forEach(System.out::println);
2 4 6 6
IntStream.of(1,2,3,3) .flatMap(i -> IntStream.of(i, i * 2)) .forEach(System.out::println);
1 2 2 4 3 6 3 6
DoubleStream? に変換する
IntStream? に変換する
LongStrea? に変換する
IntStream.of(3,2,1,3) .sorted() .forEach(System.out::println);
1 2 3 3
IntStream.of(1,2,3,3) .limit(2) .forEach(System.out::println);
1 2
System.out.println(IntStream.of(1,2,3,3).allMatch(i -> i > 0)); System.out.println(IntStream.of(1,2,3,3).allMatch(i -> i > 1)); System.out.println("--"); System.out.println(IntStream.of(1,2,3,3).anyMatch(i -> i > 1)); System.out.println(IntStream.of(1,2,3,3).anyMatch(i -> i > 3)); System.out.println("--"); System.out.println(IntStream.of(1,2,3,3).noneMatch(i -> i > 1)); System.out.println(IntStream.of(1,2,3,3).noneMatch(i -> i > 3)); System.out.println("--");
true false -- true false -- false true --
System.out.println(IntStream.of(1,2,3,3).count());
4
System.out.println(IntStream.of(1,2,3,3).sum());
9
System.out.println(IntStream.of(1,2,3,3).reduce(1, (a,b) -> a * b));
18
IntStream.of(1,2,3,3) .forEach(System.out::println);
1 2 3 3
System.out.println(Stream.of(1,2,3,3).collect(Collectors.minBy((a,b) -> a.compareTo(b)))); System.out.println(Stream.of(1,2,3,3).collect(Collectors.maxBy((a,b) -> a.compareTo(b)))); System.out.println(Stream.of("1","2","3","3").collect(Collectors.joining(",")));
Optional[1] Optional[3] 1,2,3,3
List<Integer> set = Stream.of(1,2,3,3).collect(Collectors.toList()); System.out.println(list);
[1, 2, 3]
Set<Integer> set = Stream.of(1,2,3,3).collect(Collectors.toSet()); System.out.println(set);
[1, 2, 3]
Map<Character, String> map = Stream.of("Zach","Albert","Donna","Erich","Brenda") .collect(Collectors.toMap( s -> s.charAt(0), s -> s )); System.out.println(map);
{A=Albert, B=Brenda, D=Donna, E=Erich, Z=Zach}
Map<Integer, List<String>> map2 = Stream.of("Zach","Albert","Donna","Erich","Brenda") .collect(Collectors.groupingBy( s -> s.length() )); System.out.println(map2);
{4=[Zach], 5=[Donna, Erich], 6=[Albert, Brenda]}
double pi = LongStream.range(0L, 10_000_000L) .parallel() .map(i -> ((i & 1) == 0 ? 1 : -1) * (2 * i + 1)) .mapToDouble(i -> 4.0 / i) .sum(); System.out.println(pi);
3.1415925535897933