本文整理汇总了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();
}
示例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");
}
示例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);
}
}
示例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));
}
示例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));
}