Grouping by collector java (#11001)
* Initial Commit - Ports and Adapters * Restructured code to implement input and outbou ports and adapters * Added tests for groupingBy Collector * Revert "Initial Commit - Ports and Adapters" This reverts commit 55152dea * Added tests for groupingBy Collector * reverting * corrected typo * used BDD naming strategy for tests * Added a test for groupingByConcurrent * added new module and package * added core-java-streams-4 module in main pom.xml * updated pom.xml and changed artifactId * removed gitignore and README.md Converted spaces to tabs and reformatted code * removed gitignore and README.md * used baeldung formatter
This commit is contained in:
parent
dee85d33c1
commit
bd4beba1f0
@ -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)
|
|
@ -10,8 +10,40 @@ import java.util.stream.Collectors;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class StreamGroupingByCollectorUnitTest
|
public class StreamGroupingByCollectorUnitTest {
|
||||||
{
|
@Test
|
||||||
|
public void givenListOfStrings_whenGroupingEqualStrings_thenUseCollectorsGroupingByToGroupEqualStringsAndCountOfOccurrences() {
|
||||||
|
|
||||||
|
List<String> list = new ArrayList<>(Arrays.asList("Foo", "Bar", "Bar", "Foo", "Bar"));
|
||||||
|
|
||||||
|
Map<String, Long> 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"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfStrings_whenGroupingEqualLengthStrings_thenUseCollectorsGroupingByConcurrentToGroupEqualLengthStringsAndCountOfOccurrences() {
|
||||||
|
|
||||||
|
List<String> list = new ArrayList<>(Arrays.asList("Adam", "Bill", "Jack", "Joe", "Ian"));
|
||||||
|
|
||||||
|
Map<Integer, Long> 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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfEmployees_whenGroupingDepartmentId_thenUseCollectorsGroupingByDepartmentIdAndCountNumberOfEmployeesWithinEveryDepartment() {
|
||||||
|
|
||||||
|
List<Employee> 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<Integer, Long> 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 {
|
static class Employee {
|
||||||
|
|
||||||
Integer employeeId;
|
Integer employeeId;
|
||||||
@ -49,51 +81,4 @@ public class StreamGroupingByCollectorUnitTest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void
|
|
||||||
givenListOfStrings_whenGroupingEqualStrings_thenUseCollectorsGroupingByToGroupEqualStringsAndCountOfOccurrences() {
|
|
||||||
List<String> list = new ArrayList<>(
|
|
||||||
Arrays.asList("Foo", "Bar", "Bar", "Foo", "Bar")
|
|
||||||
);
|
|
||||||
|
|
||||||
Map<String, Long> 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"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void
|
|
||||||
givenListOfStrings_whenGroupingEqualLengthStrings_thenUseCollectorsGroupingByConcurrentToGroupEqualLengthStringsAndCountOfOccurrences() {
|
|
||||||
|
|
||||||
List<String> list = new ArrayList<>(
|
|
||||||
Arrays.asList("Adam", "Bill", "Jack", "Joe", "Ian")
|
|
||||||
);
|
|
||||||
|
|
||||||
Map<Integer, Long> 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));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void
|
|
||||||
givenListOfEmployees_whenGroupingDepartmentId_thenUseCollectorsGroupingByDepartmentIdAndCountNumberOfEmployeesWithinEveryDepartment() {
|
|
||||||
|
|
||||||
List<Employee> 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<Integer, Long> 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));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
*.class
|
|
||||||
|
|
||||||
#folders#
|
|
||||||
/target
|
|
||||||
/neoDb*
|
|
||||||
/data
|
|
||||||
/src/main/webapp/WEB-INF/classes
|
|
||||||
*/META-INF/*
|
|
||||||
|
|
||||||
# Packaged files #
|
|
||||||
*.jar
|
|
||||||
*.war
|
|
||||||
*.ear
|
|
Loading…
x
Reference in New Issue
Block a user