From 57845ff4ac5f49c71443918f82432e45b4649fc1 Mon Sep 17 00:00:00 2001 From: Alexander Kobelev Date: Thu, 6 Jun 2024 15:44:56 +0300 Subject: [PATCH] Remove unneeded code --- docker-compose.yaml | 1 - .../ak/app/CollectionsAndCollectorsTest.java | 143 ------------------ 2 files changed, 144 deletions(-) delete mode 100644 src/test/java/com/ak/app/CollectionsAndCollectorsTest.java diff --git a/docker-compose.yaml b/docker-compose.yaml index 958bab2..872388e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,3 @@ -version: "3.9" services: simple-app: container_name: simple-app diff --git a/src/test/java/com/ak/app/CollectionsAndCollectorsTest.java b/src/test/java/com/ak/app/CollectionsAndCollectorsTest.java deleted file mode 100644 index ce4ac29..0000000 --- a/src/test/java/com/ak/app/CollectionsAndCollectorsTest.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.ak.app; - -import org.junit.jupiter.api.Test; - -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; - -class CollectionsAndCollectorsTest { - record Employee(String firstName, String lastName, String position, int salary) { - } - - private final List employees = List.of( - new Employee("AAA", "BBB", "developer", 10_000), - new Employee("AAB", "BBC", "architect", 15_000), - new Employee("AAC", "BBD", "developer", 13_000), - new Employee("AAD", "BBE", "tester", 7_000), - new Employee("AAE", "BBF", "tester", 9_000) - ); - - /** - * - * Interesting Facts About Java Streams and Collections - * - */ - @Test - void groupingAndAggregations() { - var m = employees.stream().collect( - Collectors.groupingBy( - Employee::position, - Collectors.summingInt(Employee::salary) - ) - ); - - assertThat(m).hasSize(3) - .contains(entry("developer", 23000)) - .contains(entry("architect", 15000)) - .contains(entry("tester", 16000)); - - var p = employees.stream().collect(Collectors.partitioningBy(e -> e.salary > 10_000)); - assertThat(p).hasSize(2) - .hasEntrySatisfying(Boolean.TRUE, es -> assertThat(es).hasSize(2)) - .hasEntrySatisfying(Boolean.FALSE, es -> assertThat(es).hasSize(3)); - } - - /** - *

- * Guide to Java 8 Collectors: groupingByConcurrent() - *

- * - *

- * Практические примеры использования Stream API - *

- */ - @Test - void groupingByConcurrent() { - record Book(String title, String author, int releaseYear) { - } - - String georgeOrwell = "George Orwell"; - String tolkien = "J.R.R. Tolkien"; - String williamGolding = "William Golding"; - List books = Arrays.asList( - new Book("The Lord of the Rings", tolkien, 1954), - new Book("The Hobbit", tolkien, 1937), - new Book("Animal Farm", georgeOrwell, 1945), - new Book("Nineteen Eighty-Four", georgeOrwell, 1949), - new Book("The Road to Wigan Pier", georgeOrwell, 1937), - new Book("Lord of the Flies", williamGolding, 1954) - ); - - ConcurrentMap> booksByAuthor = books.parallelStream() - .collect( - Collectors.groupingByConcurrent(Book::author, ConcurrentHashMap::new, Collectors.mapping(Book::title, Collectors.toList())) - ); - assertThat(booksByAuthor) - .hasEntrySatisfying(georgeOrwell, strings -> assertThat(strings).hasSize(3)) - .hasEntrySatisfying(tolkien, strings -> assertThat(strings).hasSize(2)) - .hasEntrySatisfying(williamGolding, strings -> assertThat(strings).hasSize(1)); - } - - /** - * Java-get most common element in a list - */ - @Test - void mostCommonElement() { - var v = Stream.of(1, 3, 4, 3, 2, 3, 3, 3, 3, 4) - .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) - .entrySet().stream().max(Map.Entry.comparingByValue()).orElseThrow(); - assertThat(v.getKey()).isEqualTo(3); - assertThat(v.getValue()).isEqualTo(6); - } - - @Test - void maxElementKey() { - var map = Map.of("1", 1, "2", 2, "3", 3); - String maxKey = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey(); - assertThat(maxKey).isEqualTo("3"); - } - - /** - * Hidden gems in Java 16 and Java 17, from Stream.mapMulti to HexFormat - */ - @Test - void mapMulti() { - List strings = Stream.of("Java", "Valhalla", "Panama", "Loom", "Amber") - .mapMulti((BiConsumer>) (s, mapper) -> { - if (s.length() >= 5) { - mapper.accept(s); - } - }) - .toList(); - assertThat(strings).containsExactly("Valhalla", "Panama", "Amber"); - } - - /** - * - * Interesting Facts About Java Streams and Collections - * - */ - @Test - void mapMerge() { - var map = new HashMap(); - var nums = List.of(2, 3, 4, 2, 3, 5, 1, 3, 4, 4); - nums.forEach(num -> map.merge(num, 1, Integer::sum)); - assertThat(map).hasSize(5).contains(entry(4, 3)); - } - - @Test - void mapMerge2() { - var map = new HashMap(); - employees.forEach(emp -> map.merge(emp.position(), emp.salary(), Integer::sum)); - assertThat(map).hasSize(3).contains(entry("developer", 23000)); - } -} \ No newline at end of file