Java Code Examples for java.util.stream.Collectors#groupingBy()

The following examples show how to use java.util.stream.Collectors#groupingBy() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 2
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 3
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 4
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 5
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 6
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 7
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 8
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 9
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 10
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 11
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 12
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 13
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testOps(String name, TestData.OfRef<Integer> data) {
    // @@@ More things to test here:
    //     - Every value in data is present in right bucket
    //     - Total number of values equals size of data

    for (MapperData<Integer, ?> md : getMapperData(data)) {
        Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m);
        Map<Object, List<Integer>> result =
                withData(data)
                .terminal(s -> s, s -> s.collect(tab))
                .resultAsserter((act, exp, ord, par) -> {
                    if (par & !ord) {
                        GroupByOpTest.assertMultiMapEquals(act, exp);
                    }
                    else {
                        GroupByOpTest.assertObjectEquals(act, exp);
                    }
                })
                .exercise();
        assertEquals(result.keySet().size(), md.expectedSize);
    }
}
 
Example 14
Source Project: streamex   File: MoreCollectorsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMapping() {
    assertThrows(NullPointerException.class, () -> MoreCollectors.mapping(null, Collectors.toList()));
    assertThrows(NullPointerException.class, () -> MoreCollectors.mapping(Function.identity(), null));
    List<String> input = asList("Capital", "lower", "Foo", "bar");
    Collector<String, ?, Map<Boolean, Optional<Integer>>> collector = MoreCollectors
            .partitioningBy(str -> Character.isUpperCase(str.charAt(0)), MoreCollectors.mapping(String::length,
                MoreCollectors.first()));
    checkShortCircuitCollector("mapping", new BooleanMap<>(Optional.of(7), Optional.of(5)), 2, input::stream,
        collector);
    Collector<String, ?, Map<Boolean, Optional<Integer>>> collectorLast = MoreCollectors.partitioningBy(
        str -> Character.isUpperCase(str.charAt(0)), MoreCollectors.mapping(String::length, MoreCollectors.last()));
    checkCollector("last", new BooleanMap<>(Optional.of(3), Optional.of(3)), input::stream, collectorLast);

    input = asList("Abc", "Bac", "Aac", "Abv", "Bbc", "Bgd", "Atc", "Bpv");
    Map<Character, List<String>> expected = EntryStream.of('A', asList("Abc", "Aac"), 'B', asList("Bac", "Bbc"))
            .toMap();
    AtomicInteger cnt = new AtomicInteger();
    Collector<String, ?, Map<Character, List<String>>> groupMap = Collectors.groupingBy(s -> s.charAt(0),
        MoreCollectors.mapping(x -> {
            cnt.incrementAndGet();
            return x;
        }, MoreCollectors.head(2)));
    checkCollector("groupMap", expected, input::stream, groupMap);
    cnt.set(0);
    assertEquals(expected, input.stream().collect(groupMap));
    assertEquals(4, cnt.get());

    checkCollector("mapping-toList", asList("a", "b", "c"), asList("a1", "b2", "c3")::stream, MoreCollectors
            .mapping(str -> str.substring(0, 1)));
}
 
Example 15
private static Collector<Configuration, ?, Map<ConfigurationKeys.SettingCategory, List<Configuration>>> groupByCategory() {
    return Collectors.groupingBy(c -> c.getConfigurationKey().getCategory());
}
 
Example 16
Source Project: jsr354-ri   File: MonetaryFunctions.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Collector to group by CurrencyUnit
 * @return the Collector to of Map<CurrencyUnit, List<MonetaryAmount>>
 */
public static Collector<MonetaryAmount,?,Map<CurrencyUnit,List<MonetaryAmount>>> groupByCurrencyUnit(){
    return Collectors.groupingBy(MonetaryAmount::getCurrency);
}