本文整理匯總了Java中java.util.stream.Collector類的典型用法代碼示例。如果您正苦於以下問題:Java Collector類的具體用法?Java Collector怎麽用?Java Collector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Collector類屬於java.util.stream包,在下文中一共展示了Collector類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testToImmutableTable
import java.util.stream.Collector; //導入依賴的package包/類
public void testToImmutableTable() {
Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector =
ImmutableTable.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue);
Equivalence<ImmutableTable<String, String, Integer>> equivalence =
Equivalence.equals()
.<Cell<String, String, Integer>>pairwise()
.onResultOf(ImmutableTable::cellSet);
CollectorTester.of(collector, equivalence)
.expectCollects(
new ImmutableTable.Builder<String, String, Integer>()
.put("one", "uno", 1)
.put("two", "dos", 2)
.put("three", "tres", 3)
.build(),
Tables.immutableCell("one", "uno", 1),
Tables.immutableCell("two", "dos", 2),
Tables.immutableCell("three", "tres", 3));
}
示例2: should_create_the_correct_stream_on_groupingBy_then_all_max_with_several_maxes_in_a_set
import java.util.stream.Collector; //導入依賴的package包/類
@Test @SuppressWarnings("unchecked")
public void should_create_the_correct_stream_on_groupingBy_then_all_max_with_several_maxes_in_a_set() {
// Given
Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "three", "four", "four", "four");
Collector<String, ?, Set<Map.Entry<String, Long>>> collector =
CollectorsUtils.groupingByAndAllMaxBy(
identity(),
HashSet::new,
Collectors.counting(),
Map.Entry.comparingByValue()
);
// When
Set<Map.Entry<String, Long>> result = strings.collect(collector);
// Then
assertThat(result.size()).isEqualTo(2);
assertThat(result).isExactlyInstanceOf(HashSet.class);
assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("three", 3L));
assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("four", 3L));
}
示例3: should_return_an_empty_optional_for_groupingBy_and_maxBy_on_an_empty_stream
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_return_an_empty_optional_for_groupingBy_and_maxBy_on_an_empty_stream() {
// Given
Stream<String> strings = Stream.empty();
Collector<String, ?, Optional<Map.Entry<String, Long>>> collector =
CollectorsUtils.groupingByAndMaxBy(
identity(),
counting(),
Map.Entry.comparingByValue()
);
// When
Optional<Map.Entry<String, Long>> result = strings.collect(collector);
// Then
assertThat(result.isPresent()).isFalse();
}
示例4: should_return_an_empty_optional_for_groupingBy_and_max_by_value_with_a_comparator_on_an_empty_stream
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_return_an_empty_optional_for_groupingBy_and_max_by_value_with_a_comparator_on_an_empty_stream() {
// Given
Stream<String> strings = Stream.empty();
Collector<String, ?, Optional<Map.Entry<String, Long>>> collector =
CollectorsUtils.groupingByAndMaxByValue(
identity(),
counting(),
Comparator.reverseOrder()
);
// When
Optional<Map.Entry<String, Long>> result = strings.collect(collector);
// Then
assertThat(result.isPresent()).isFalse();
}
示例5: should_create_the_correct_stream_on_groupingBy_for_an_empty_stream
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_create_the_correct_stream_on_groupingBy_for_an_empty_stream() {
// Given
Stream<String> strings = Stream.empty();
Collector<String, ?, Stream<Map.Entry<Integer, List<String>>>> groupingByThenStream =
CollectorsUtils.groupingByThenStream(
String::length
);
// When
List<Map.Entry<Integer, List<String>>> entries =
strings.collect(groupingByThenStream).collect(Collectors.toList());
// Then
assertThat(entries).isEmpty();
}
示例6: toMultiset
import java.util.stream.Collector; //導入依賴的package包/類
/**
* Returns a {@code Collector} that accumulates elements into a multiset created via the specified
* {@code Supplier}, whose elements are the result of applying {@code elementFunction} to the
* inputs, with counts equal to the result of applying {@code countFunction} to the inputs.
* Elements are added in encounter order.
*
* <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the element
* will be added more than once, with the count summed over all appearances of the element.
*
* <p>Note that {@code stream.collect(toMultiset(function, e -> 1, supplier))} is equivalent to
* {@code stream.map(function).collect(Collectors.toCollection(supplier))}.
*
* @since 22.0
*/
public static <T, E, M extends Multiset<E>> Collector<T, ?, M> toMultiset(
java.util.function.Function<T, E> elemFunction,
java.util.function.ToIntFunction<T> countFunction,
java.util.function.Supplier<M> implSupplier) {
checkNotNull(elemFunction);
checkNotNull(countFunction);
checkNotNull(implSupplier);
return Collector.of(
implSupplier,
(ms, t) -> ms.add(elemFunction.apply(t), countFunction.applyAsInt(t)),
(ms1, ms2) -> {
ms1.addAll(ms2);
return ms1;
});
}
示例7: assertThat
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_collect_flat_map_with_a_downstream_collector_and_a_flat_mapper_a_non_empty_stream_into_a_stream() {
// Given
Stream<String> strings = Stream.of("one", "two", "three");
Function<String, Stream<Character>> streamMapper = string -> string.chars().mapToObj(letter -> (char)letter);
Collector<Stream<Character>, ?, Stream<Stream<Character>>> downstream = CollectorsUtils.mapToStream();
Function<Stream<Character>, Stream<String>> mapper = stream -> stream.map(letter -> Character.toString(letter));
Collector<String, ?, Stream<String>> streamCollector = CollectorsUtils.flatMapping(streamMapper, downstream, mapper);
// When
List<String> characters = strings.collect(streamCollector).collect(toList());
// Then
assertThat(characters.size()).isEqualTo(11);
assertThat(characters).containsExactly("o", "n", "e", "t", "w", "o", "t", "h", "r", "e", "e");
}
示例8: should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_one_max_and_n_bigger
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_one_max_and_n_bigger() {
// Given
Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "four", "four", "four");
Collector<String, ?, List<Map.Entry<String, Long>>> collector =
CollectorsUtils.groupingByAndMaxesBy(
identity(),
counting(),
10,
Map.Entry.comparingByValue()
);
// When
List<Map.Entry<String, Long>> result = strings.collect(collector);
// Then
assertThat(result.size()).isEqualTo(4);
assertThat(result).containsExactly(
new AbstractMap.SimpleImmutableEntry<>("four", 3L),
new AbstractMap.SimpleImmutableEntry<>("three", 2L),
new AbstractMap.SimpleImmutableEntry<>("two", 2L),
new AbstractMap.SimpleImmutableEntry<>("one", 1L)
);
}
示例9: should_stream_a_empty_stream_with_a_collection_mapper_into_the_correct_stream
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_stream_a_empty_stream_with_a_collection_mapper_into_the_correct_stream() {
// Given
Stream<String> strings = Stream.of("Hello", "world");
Function<String, List<Character>> mapper = (String s) -> s.chars().mapToObj(letter -> (char)letter).collect(toList());
// When
Collector<String, ?, Stream<Stream<Character>>> collector = CollectorsUtils.mapToStream(mapper);
Stream<Stream<Character>> result = strings.collect(collector);
List<List<Character>> list = result.map(stream -> stream.collect(toList())).collect(toList());
// Then
assertThat(list.size()).isEqualTo(2);
assertThat(list.get(0)).containsExactly('H', 'e', 'l', 'l', 'o');
assertThat(list.get(1)).containsExactly('w', 'o', 'r', 'l', 'd');
}
示例10: should_create_the_correct_stream_on_groupingBy_with_a_downstream_for_an_empty_stream
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_create_the_correct_stream_on_groupingBy_with_a_downstream_for_an_empty_stream() {
// Given
Stream<String> strings = Stream.empty();
Collector<String, ?, Stream<Map.Entry<Integer, Long>>> groupingByThenStream =
CollectorsUtils.groupingByThenStream(
String::length, counting()
);
// When
List<Map.Entry<Integer, Long>> entries =
strings.collect(groupingByThenStream).collect(Collectors.toList());
// Then
assertThat(entries).isEmpty();
}
示例11: testFlatteningToImmutableSetMultimap
import java.util.stream.Collector; //導入依賴的package包/類
public void testFlatteningToImmutableSetMultimap() {
Collector<String, ?, ImmutableSetMultimap<Character, Character>> collector =
ImmutableSetMultimap.flatteningToImmutableSetMultimap(
str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c));
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet()))
.and(Equivalence.equals());
ImmutableSetMultimap<Character, Character> empty = ImmutableSetMultimap.of();
ImmutableSetMultimap<Character, Character> filled =
ImmutableSetMultimap.<Character, Character>builder()
.putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
.putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
.putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
.putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
.putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
.build();
CollectorTester.of(collector, equivalence)
.expectCollects(empty)
.expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry");
}
示例12: should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_several_maxes_more_n_results
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_several_maxes_more_n_results() {
// Given
Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "four", "four");
Collector<String, ?, List<Map.Entry<String, Long>>> collector =
CollectorsUtils.groupingByAndMaxesBy(
identity(),
counting(),
2,
Map.Entry.comparingByValue()
);
// When
List<Map.Entry<String, Long>> result = strings.collect(collector);
// Then
assertThat(result.size()).isEqualTo(3);
assertThat(result).containsExactly(
new AbstractMap.SimpleImmutableEntry<>("four", 2L),
new AbstractMap.SimpleImmutableEntry<>("three", 2L),
new AbstractMap.SimpleImmutableEntry<>("two", 2L)
);
}
示例13: should_create_the_correct_stream_on_groupingBy_then_all_max_by_value_with_several_maxes
import java.util.stream.Collector; //導入依賴的package包/類
@Test @SuppressWarnings("unchecked")
public void should_create_the_correct_stream_on_groupingBy_then_all_max_by_value_with_several_maxes() {
// Given
Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "three", "four", "four", "four");
Collector<String, ?, Set<Map.Entry<String, Long>>> collector =
CollectorsUtils.groupingByAndAllMaxByValue(
Function.<String>identity(),
counting()
);
// When
Set<Map.Entry<String, Long>> result = strings.collect(collector);
// Then
assertThat(result.size()).isEqualTo(2);
assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("three", 3L));
assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("four", 3L));
}
示例14: should_collect_flat_map_with_a_downstream_collector_a_non_empty_stream_into_a_stream
import java.util.stream.Collector; //導入依賴的package包/類
@Test
public void should_collect_flat_map_with_a_downstream_collector_a_non_empty_stream_into_a_stream() {
// Given
Stream<String> strings = Stream.of("one", "two", "three");
Function<String, Stream<Character>> streamMapper = string -> string.chars().mapToObj(letter -> (char)letter);
Collector<Stream<Character>, ?, Stream<Stream<Character>>> downstream = CollectorsUtils.mapToStream();
Collector<String, ?, Stream<Character>> streamCollector = CollectorsUtils.flatMapping(streamMapper, downstream);
// When
List<Character> characters = strings.collect(streamCollector).collect(toList());
// Then
assertThat(characters.size()).isEqualTo(11);
assertThat(characters).containsExactly('o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e');
}
示例15: testToImmutableSortedMapMerging
import java.util.stream.Collector; //導入依賴的package包/類
public void testToImmutableSortedMapMerging() {
Collector<Entry<String, Integer>, ?, ImmutableSortedMap<String, Integer>> collector =
ImmutableSortedMap.toImmutableSortedMap(
Comparator.naturalOrder(), Entry::getKey, Entry::getValue, Integer::sum);
Equivalence<ImmutableMap<String, Integer>> equivalence =
Equivalence.equals()
.<Entry<String, Integer>>pairwise()
.onResultOf(ImmutableMap::entrySet);
CollectorTester.of(collector, equivalence)
.expectCollects(
ImmutableSortedMap.of("one", 1, "three", 3, "two", 4),
mapEntry("one", 1),
mapEntry("two", 2),
mapEntry("three", 3),
mapEntry("two", 2));
}