├── .github └── FUNDING.yml ├── .gitignore ├── pom.xml └── src ├── main └── java │ └── letscode │ └── javalearn │ └── domain │ ├── Department.java │ ├── Employee.java │ ├── Event.java │ └── Position.java └── test └── java └── letscode └── javalearn └── Streams.java /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: letscodedru 2 | custom: ["https://money.yandex.ru/to/41001451675086", "https://paypal.me/letscodedru", "https://qiwi.me/letscode", "https://donate.stream/mrdru"] 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.jar 3 | *.iml 4 | target 5 | .idea 6 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | letscode 8 | java-learn 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 9 17 | 9 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | junit 26 | junit 27 | LATEST 28 | 29 | 30 | 31 | org.projectlombok 32 | lombok 33 | LATEST 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/main/java/letscode/javalearn/domain/Department.java: -------------------------------------------------------------------------------- 1 | package letscode.javalearn.domain; 2 | 3 | import lombok.Data; 4 | import lombok.RequiredArgsConstructor; 5 | import lombok.ToString; 6 | 7 | import java.util.HashSet; 8 | import java.util.Set; 9 | 10 | @Data 11 | @RequiredArgsConstructor 12 | @ToString(of = {"id", "child"}) 13 | public class Department { 14 | private final int id; 15 | private final int parent; 16 | private final String name; 17 | 18 | private Set child = new HashSet<>(); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/letscode/javalearn/domain/Employee.java: -------------------------------------------------------------------------------- 1 | package letscode.javalearn.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class Employee { 9 | private String firstName; 10 | private String lastName; 11 | private int id; 12 | private int age; 13 | private Position position; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/letscode/javalearn/domain/Event.java: -------------------------------------------------------------------------------- 1 | package letscode.javalearn.domain; 2 | 3 | import java.time.LocalDateTime; 4 | import java.util.UUID; 5 | 6 | public class Event { 7 | private UUID id; 8 | private LocalDateTime timeTag; 9 | private String description; 10 | 11 | public Event(UUID id, LocalDateTime timeTag, String description) { 12 | this.id = id; 13 | this.timeTag = timeTag; 14 | this.description = description; 15 | 16 | System.out.printf("Generated %s", id.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/letscode/javalearn/domain/Position.java: -------------------------------------------------------------------------------- 1 | package letscode.javalearn.domain; 2 | 3 | public enum Position { 4 | MANAGER, WORKER, CHEF; 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/letscode/javalearn/Streams.java: -------------------------------------------------------------------------------- 1 | package letscode.javalearn; 2 | 3 | import letscode.javalearn.domain.Department; 4 | import letscode.javalearn.domain.Employee; 5 | import letscode.javalearn.domain.Event; 6 | import letscode.javalearn.domain.Position; 7 | import org.junit.Test; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | import java.io.Serializable; 12 | import java.nio.file.Files; 13 | import java.nio.file.Path; 14 | import java.nio.file.Paths; 15 | import java.time.LocalDateTime; 16 | import java.util.*; 17 | import java.util.stream.*; 18 | 19 | public class Streams { 20 | private List emps = List.of( 21 | new Employee("Michael", "Smith", 243, 43, Position.CHEF), 22 | new Employee("Jane", "Smith", 523, 40, Position.MANAGER), 23 | new Employee("Jury", "Gagarin", 6423, 26, Position.MANAGER), 24 | new Employee("Jack", "London", 5543, 53, Position.WORKER), 25 | new Employee("Eric", "Jackson", 2534, 22, Position.WORKER), 26 | new Employee("Andrew", "Bosh", 3456, 44, Position.WORKER), 27 | new Employee("Joe", "Smith", 723, 30, Position.MANAGER), 28 | new Employee("Jack", "Gagarin", 7423, 35, Position.MANAGER), 29 | new Employee("Jane", "London", 7543, 42, Position.WORKER), 30 | new Employee("Mike", "Jackson", 7534, 31, Position.WORKER), 31 | new Employee("Jack", "Bosh", 7456, 54, Position.WORKER), 32 | new Employee("Mark", "Smith", 123, 41, Position.MANAGER), 33 | new Employee("Jane", "Gagarin", 1423, 28, Position.MANAGER), 34 | new Employee("Sam", "London", 1543, 52, Position.WORKER), 35 | new Employee("Jack", "Jackson", 1534, 27, Position.WORKER), 36 | new Employee("Eric", "Bosh", 1456, 32, Position.WORKER) 37 | ); 38 | 39 | private List deps = List.of( 40 | new Department(1, 0, "Head"), 41 | new Department(2, 1, "West"), 42 | new Department(3, 1, "East"), 43 | new Department(4, 2, "Germany"), 44 | new Department(5, 2, "France"), 45 | new Department(6, 3, "China"), 46 | new Department(7, 3, "Japan") 47 | ); 48 | 49 | @Test 50 | public void creation() throws IOException { 51 | Stream lines = Files.lines(Paths.get("some.txt")); 52 | Stream list = Files.list(Paths.get("./")); 53 | Stream walk = Files.walk(Paths.get("./"), 3); 54 | 55 | IntStream intStream = IntStream.of(1, 2, 3, 4); 56 | DoubleStream doubleStream = DoubleStream.of(1.2, 3.4); 57 | IntStream range = IntStream.range(10, 100); // 10 .. 99 58 | IntStream intStream1 = IntStream.rangeClosed(10, 100); // 10 .. 100 59 | 60 | int[] ints = {1, 2, 3, 4}; 61 | IntStream stream = Arrays.stream(ints); 62 | 63 | Stream stringStream = Stream.of("1", "2", "3"); 64 | Stream stream1 = Stream.of(1, "2", "3"); 65 | 66 | Stream build = Stream.builder() 67 | .add("Mike") 68 | .add("joe") 69 | .build(); 70 | 71 | Stream stream2 = emps.stream(); 72 | Stream employeeStream = emps.parallelStream(); 73 | 74 | Stream generate = Stream.generate(() -> 75 | new Event(UUID.randomUUID(), LocalDateTime.now(), "") 76 | ); 77 | 78 | Stream iterate = Stream.iterate(1950, val -> val + 3); 79 | 80 | Stream concat = Stream.concat(stringStream, build); 81 | } 82 | 83 | @Test 84 | public void terminate() { 85 | Stream stream = emps.stream(); 86 | stream.count(); 87 | 88 | emps.stream().forEach(employee -> System.out.println(employee.getAge())); 89 | emps.forEach(employee -> System.out.println(employee.getAge())); 90 | 91 | emps.stream().forEachOrdered(employee -> System.out.println(employee.getAge())); 92 | 93 | emps.stream().collect(Collectors.toList()); 94 | emps.stream().toArray(); 95 | Map collect = emps.stream().collect(Collectors.toMap( 96 | Employee::getId, 97 | emp -> String.format("%s %s", emp.getLastName(), emp.getFirstName()) 98 | )); 99 | 100 | IntStream intStream = IntStream.of(100, 200, 300, 400); 101 | intStream.reduce((left, right) -> left + right).orElse(0); 102 | 103 | System.out.println(deps.stream().reduce(this::reducer)); 104 | 105 | IntStream.of(100, 200, 300, 400).average(); 106 | IntStream.of(100, 200, 300, 400).max(); 107 | IntStream.of(100, 200, 300, 400).min(); 108 | IntStream.of(100, 200, 300, 400).sum(); 109 | IntStream.of(100, 200, 300, 400).summaryStatistics(); 110 | 111 | emps.stream().max(Comparator.comparingInt(Employee::getAge)); 112 | 113 | emps.stream().findAny(); 114 | emps.stream().findFirst(); 115 | 116 | emps.stream().noneMatch(employee -> employee.getAge() > 60); // true 117 | emps.stream().anyMatch(employee -> employee.getPosition() == Position.CHEF); // true 118 | emps.stream().allMatch(employee -> employee.getAge() > 18); // true 119 | } 120 | 121 | @Test 122 | public void transform() { 123 | LongStream longStream = IntStream.of(100, 200, 300, 400).mapToLong(Long::valueOf); 124 | IntStream.of(100, 200, 300, 400).mapToObj(value -> 125 | new Event(UUID.randomUUID(), LocalDateTime.of(value, 12, 1, 12, 0), "") 126 | ); 127 | 128 | IntStream.of(100, 200, 300, 400, 100, 200).distinct(); // 100, 200, 300, 400 129 | 130 | Stream employeeStream = emps.stream().filter(employee -> employee.getPosition() != Position.CHEF); 131 | 132 | emps.stream() 133 | .skip(3) 134 | .limit(5); 135 | 136 | emps.stream() 137 | .sorted(Comparator.comparingInt(Employee::getAge)) 138 | .peek(emp -> emp.setAge(18)) 139 | .map(emp -> String.format("%s %s", emp.getLastName(), emp.getFirstName())); 140 | 141 | emps.stream().takeWhile(employee -> employee.getAge() > 30).forEach(System.out::println); 142 | System.out.println(); 143 | emps.stream().dropWhile(employee -> employee.getAge() > 30).forEach(System.out::println); 144 | 145 | System.out.println(); 146 | 147 | IntStream.of(100, 200, 300, 400) 148 | .flatMap(value -> IntStream.of(value - 50, value)) 149 | .forEach(System.out::println); 150 | } 151 | 152 | @Test 153 | public void real() { 154 | Stream empl = emps.stream() 155 | .filter(employee -> 156 | employee.getAge() <= 30 && employee.getPosition() != Position.WORKER 157 | ) 158 | .sorted(Comparator.comparing(Employee::getLastName)); 159 | 160 | print(empl); 161 | 162 | Stream sorted = emps.stream() 163 | .filter(employee -> employee.getAge() > 40) 164 | .sorted((o1, o2) -> o2.getAge() - o1.getAge()) 165 | .limit(4); 166 | 167 | print(sorted); 168 | 169 | IntSummaryStatistics statistics = emps.stream() 170 | .mapToInt(Employee::getAge) 171 | .summaryStatistics(); 172 | 173 | System.out.println(statistics); 174 | } 175 | 176 | private void print(Stream stream) { 177 | stream 178 | .map(emp -> String.format( 179 | "%4d | %-15s %-10s age %s %s", 180 | emp.getId(), 181 | emp.getLastName(), 182 | emp.getFirstName(), 183 | emp.getAge(), 184 | emp.getPosition() 185 | )) 186 | .forEach(System.out::println); 187 | 188 | System.out.println(); 189 | } 190 | 191 | public Department reducer(Department parent, Department child) { 192 | if (child.getParent() == parent.getId()) { 193 | parent.getChild().add(child); 194 | } else { 195 | parent.getChild().forEach(subParent -> reducer(subParent, child)); 196 | } 197 | 198 | return parent; 199 | } 200 | } 201 | --------------------------------------------------------------------------------