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

The following examples show how to use java.util.stream.Collectors#groupingBy() . 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
Source File: GroupByOpTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: GroupByOpTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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 File: MoreCollectorsTest.java    From streamex with 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
Source File: ConfigurationManager.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
private static Collector<Configuration, ?, Map<ConfigurationKeys.SettingCategory, List<Configuration>>> groupByCategory() {
    return Collectors.groupingBy(c -> c.getConfigurationKey().getCategory());
}
 
Example 16
Source File: MonetaryFunctions.java    From jsr354-ri with 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);
}