当前位置: 首页>>代码示例>>Java>>正文


Java Collectors.groupingBy方法代码示例

本文整理汇总了Java中java.util.stream.Collectors.groupingBy方法的典型用法代码示例。如果您正苦于以下问题:Java Collectors.groupingBy方法的具体用法?Java Collectors.groupingBy怎么用?Java Collectors.groupingBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.stream.Collectors的用法示例。


在下文中一共展示了Collectors.groupingBy方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: should_create_the_correct_stream_on_toMap_with_a_collector_for_an_empty_stream

import java.util.stream.Collectors; //导入方法依赖的package包/类
@Test
public void should_create_the_correct_stream_on_toMap_with_a_collector_for_an_empty_stream() {

    // Given
    Stream<String> strings = Stream.empty();

    Collector<String, ?, Map<Integer, List<String>>> groupingBy = Collectors.groupingBy(String::length);
    Collector<String, ?, Stream<Map.Entry<Integer, List<String>>>> collector =
            CollectorsUtils.toMapThenStream(groupingBy);

    // When
    List<Map.Entry<Integer, List<String>>> entries = strings.collect(collector).collect(toList());

    // Then
    assertThat(entries).asList().isEmpty();
}
 
开发者ID:JosePaumard,项目名称:collectors-utils,代码行数:17,代码来源:ToMapThenStreamTest.java

示例2: should_create_the_correct_stream_on_toMap_with_a_collector_for_a_non_empty_stream

import java.util.stream.Collectors; //导入方法依赖的package包/类
@Test
public void should_create_the_correct_stream_on_toMap_with_a_collector_for_a_non_empty_stream() {

    // Given
    Stream<String> strings = Stream.of("one", "two", "three", "four");

    Collector<String, ?, Map<Integer, List<String>>> groupingBy = Collectors.groupingBy(String::length);
    Collector<String, ?, Stream<Map.Entry<Integer, List<String>>>> collector =
            CollectorsUtils.toMapThenStream(groupingBy);

    // When
    List<Map.Entry<Integer, List<String>>> entries = strings.collect(collector).collect(toList());
    Map<Integer, List<String>> result = entries.stream().collect(toMap(
            entry -> entry.getKey(),
            entry -> entry.getValue()
    ));

    // Then
    assertThat(result.size()).isEqualTo(3);
    assertThat(result.get(3)).asList().containsExactly("one", "two");
    assertThat(result.get(4)).asList().containsExactly("four");
    assertThat(result.get(5)).asList().containsExactly("three");
}
 
开发者ID:JosePaumard,项目名称:collectors-utils,代码行数:24,代码来源:ToMapThenStreamTest.java

示例3: testOps

import java.util.stream.Collectors; //导入方法依赖的package包/类
@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);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:GroupByOpTest.java

示例4: groupingBy

import java.util.stream.Collectors; //导入方法依赖的package包/类
public static Collector<DetailedTick, ?, Map<Integer, Byte>> groupingBy(TIME_FRAME time_frame) {
	return Collectors.groupingBy((tick) -> trim_time(tick.time, time_frame), new LiquidityCollector(time_frame));
}
 
开发者ID:zc8424,项目名称:QuantTester,代码行数:4,代码来源:LiquidityCollector.java

示例5: groupingBy

import java.util.stream.Collectors; //导入方法依赖的package包/类
public static Collector<Tick, ?, Map<Integer, FutureBar>> groupingBy(TIME_FRAME time_frame) {
	return Collectors.groupingBy((tick) -> trim_time(tick.time, time_frame), new FutureBarCollector(time_frame));
}
 
开发者ID:zc8424,项目名称:QuantTester,代码行数:4,代码来源:FutureBarCollector.java


注:本文中的java.util.stream.Collectors.groupingBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。