diff --git a/core-java-modules/core-java-streams-4/README.md b/core-java-modules/core-java-streams-4/README.md deleted file mode 100644 index 06b426d8a7..0000000000 --- a/core-java-modules/core-java-streams-4/README.md +++ /dev/null @@ -1,6 +0,0 @@ -## Core Java streams - -This module contains articles about the Stream API in Java. - -### Relevant Articles: -- [Count Occurrences using Java groupingBy Collector](https://baeldung.com/count-occurrences-with-java-groupingby-collector) diff --git a/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streamcollectors/StreamGroupingByCollectorUnitTest.java b/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streamcollectors/StreamGroupingByCollectorUnitTest.java index 74b9d61370..c50d73a638 100644 --- a/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streamcollectors/StreamGroupingByCollectorUnitTest.java +++ b/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streamcollectors/StreamGroupingByCollectorUnitTest.java @@ -10,90 +10,75 @@ import java.util.stream.Collectors; import org.junit.Assert; import org.junit.Test; -public class StreamGroupingByCollectorUnitTest -{ - static class Employee { +public class StreamGroupingByCollectorUnitTest { + @Test + public void givenListOfStrings_whenGroupingEqualStrings_thenUseCollectorsGroupingByToGroupEqualStringsAndCountOfOccurrences() { - Integer employeeId; - String employeeName; - Integer departmentId; + List list = new ArrayList<>(Arrays.asList("Foo", "Bar", "Bar", "Foo", "Bar")); - Employee(Integer employeeId, String employeeName, Integer departmentId) { - this.employeeId = employeeId; - this.employeeName = employeeName; - this.departmentId = departmentId; - } + Map result = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); + Assert.assertEquals(new Long(2), result.get("Foo")); + Assert.assertEquals(new Long(3), result.get("Bar")); - public Integer getEmployeeId() { - return employeeId; - } + } - public void setEmployeeId(Integer employeeId) { - this.employeeId = employeeId; - } + @Test + public void givenListOfStrings_whenGroupingEqualLengthStrings_thenUseCollectorsGroupingByConcurrentToGroupEqualLengthStringsAndCountOfOccurrences() { - public String getEmployeeName() { - return employeeName; - } + List list = new ArrayList<>(Arrays.asList("Adam", "Bill", "Jack", "Joe", "Ian")); - public void setEmployeeName(String employeeName) { - this.employeeName = employeeName; - } + Map result = list.stream().collect(Collectors.groupingByConcurrent(String::length, Collectors.counting())); + Assert.assertEquals(new Long(2), result.get(3)); + Assert.assertEquals(new Long(3), result.get(4)); - public Integer getDepartmentId() { - return departmentId; - } + } - public void setDepartmentId(Integer departmentId) { - this.departmentId = departmentId; - } + @Test + public void givenListOfEmployees_whenGroupingDepartmentId_thenUseCollectorsGroupingByDepartmentIdAndCountNumberOfEmployeesWithinEveryDepartment() { + + List list = new ArrayList<>(Arrays.asList(new Employee(1, "Joe", 1), new Employee(2, "Josh", 1), new Employee(3, "Jamie", 2), new Employee(4, "Jim", 2), new Employee(5, "Jack", 2))); + + Map result = list.stream().collect(Collectors.groupingBy(Employee::getDepartmentId, Collectors.counting())); + Assert.assertEquals(new Long(2), result.get(1)); + Assert.assertEquals(new Long(3), result.get(2)); + + } + + static class Employee { + + Integer employeeId; + String employeeName; + Integer departmentId; + + Employee(Integer employeeId, String employeeName, Integer departmentId) { + this.employeeId = employeeId; + this.employeeName = employeeName; + this.departmentId = departmentId; } - @Test - public void - givenListOfStrings_whenGroupingEqualStrings_thenUseCollectorsGroupingByToGroupEqualStringsAndCountOfOccurrences() { - List list = new ArrayList<>( - Arrays.asList("Foo", "Bar", "Bar", "Foo", "Bar") - ); - - Map result = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); - Assert.assertEquals(new Long(2), result.get("Foo")); - Assert.assertEquals(new Long(3), result.get("Bar")); - + public Integer getEmployeeId() { + return employeeId; } - @Test - public void - givenListOfStrings_whenGroupingEqualLengthStrings_thenUseCollectorsGroupingByConcurrentToGroupEqualLengthStringsAndCountOfOccurrences() { - - List list = new ArrayList<>( - Arrays.asList("Adam", "Bill", "Jack", "Joe", "Ian") - ); - - Map result = list.stream() - .collect(Collectors.groupingByConcurrent(String::length, Collectors.counting())); - Assert.assertEquals(new Long(2), result.get(3)); - Assert.assertEquals(new Long(3), result.get(4)); - + public void setEmployeeId(Integer employeeId) { + this.employeeId = employeeId; } - - @Test - public void - givenListOfEmployees_whenGroupingDepartmentId_thenUseCollectorsGroupingByDepartmentIdAndCountNumberOfEmployeesWithinEveryDepartment() { - - List list = new ArrayList<>( - Arrays.asList(new Employee(1, "Joe", 1), - new Employee(2, "Josh", 1), - new Employee(3, "Jamie", 2), - new Employee(4, "Jim", 2), - new Employee(5, "Jack", 2)) - ); - - Map result = list.stream() .collect(Collectors.groupingBy(Employee::getDepartmentId, Collectors.counting())); - Assert.assertEquals(new Long(2), result.get(1)); - Assert.assertEquals(new Long(3), result.get(2)); - + public String getEmployeeName() { + return employeeName; } + public void setEmployeeName(String employeeName) { + this.employeeName = employeeName; + } + + public Integer getDepartmentId() { + return departmentId; + } + + public void setDepartmentId(Integer departmentId) { + this.departmentId = departmentId; + } + } + } diff --git a/core-java-modules/core-java-streams-4/src/test/resources/.gitignore b/core-java-modules/core-java-streams-4/src/test/resources/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/core-java-modules/core-java-streams-4/src/test/resources/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file