[Java] Stream Collectors notes

Personal notes

Collectors#toMap()

I want to use LinkedHashMap

I want to use LikedHashMap explicitly. By the way, I want to allow duplicate keys.

	public static <T, K, V> Collector<T, ?, Map<K, V>> toMap(
			Function<? super T, ? extends K> keyMapper,
			Function<? super T, ? extends V> valueMapper) {

		return Collectors.toMap(
				keyMapper,
				valueMapper,
				(a, b) -> b,
				LinkedHashMap::new);
	}

Throw an exception if you don't want to allow duplicate keys.

(a, b) -> { throw new IllegalStateException(~); }

See the standard two-argument Collectors # toMap () for details.

I want to use TreeMap

I want to sort naturally by key. almost the same.

	public static <T, K, V> Collector<T, ?, Map<K, V>> toTreeMap(
			Function<? super T, ? extends K> keyMapper,
			Function<? super T, ? extends V> valueMapper) {

		return Collectors.toMap(
				keyMapper,
				valueMapper,
				(a, b) -> b,
				TreeMap::new); //★ Here
	}

When you want to specify Comparator in TreeMap, specify it with a lambda expression instead of a constructor reference.

Collectors#toSet()

I want to use LinkedHashSet

The point here is to use toCollection () instead of toSet ().

	public static <T> Collector<T, ?, Set<T>> toSet() {
		return Collectors.toCollection(LinkedHashSet::new);
	}

I want to use TreeSet

Basically the same.

	public static <T> Collector<T, ?, Set<T>> toTreeSet() {
		return Collectors.toCollection(TreeSet::new);
	}

Collectors#groupingBy

I want to use LinkedHashMap

	public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(
			Function<? super T, ? extends K> classifier) {

		return Collectors.groupingBy(classifier,
				LinkedHashMap::new, //★ Here
				Collectors.toList());
	}

I want to use TreeMap

the same.

	public static <T, K> Collector<T, ?, Map<K, List<T>>> orderedGroupBy(
			Function<? super T, ? extends K> classifier) {

		return Collectors.groupingBy(classifier,
				TreeMap::new, //★ Here
				Collectors.toList());
	}

I want the element to be a Set instead of a List

... I can't think of a good method name.

	public static <T, K> Collector<T, ?, Map<K, Set<T>>> groupingBySet(
			Function<? super T, ? extends K> classifier) {

		return Collectors.groupingBy(classifier,
				TreeMap::new,
				Collectors.toCollection(LinkedHashSet::new)); //★ Here
	}

Of course, TreeSet or ordinary Collectors.toSet () can be used depending on the purpose.

I also want to convert the elements to be packed in the List when grouping

	public static <T, K, D> Collector<T, ?, Map<K, List<D>>> groupingBy(
			Function<? super T, ? extends K> classifier,
			Function<? super T, ? extends D> mapper) {

		return Collectors.groupingBy(classifier,
				LinkedHashMap::new,
				Collectors.mapping(mapper, //★ Here
						Collectors.toList()));
	}

Of course, you can use Set instead of List.

Collectors#collectingAndThen

If you want to generate an immutable Map or List.

	public static <T, K, V> Collector<T, ?, Map<K, V>> toConstMap(
			Function<? super T, ? extends K> keyMapper,
			Function<? super T, ? extends V> valueMapper) {

		return Collectors.collectingAndThen(
				Collectors.toMap(
					keyMapper,
					valueMapper),
				Collections::unmodifiableMap);
	}

Recommended Posts

[Java] Stream Collectors notes
java notes
[JAVA] Stream type
Java Generics (Notes)
Try Java 8 Stream
[Java] Array notes
Java Stream API
[Java] Study notes
Java serialization notes
Studying Java 8 (Stream)
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
Java formatted output [Notes]
Studying Java 8 (Collector / Collectors)
[Java] Stream API-Stream generation
Stream API (Collectors class)
[Java] Stream API / map
[Java] Control syntax notes
Java NIO 2 review notes
Java8 Stream API practice
Java8 Stream reduction operation
[Java] Basic method notes
Java8 Stream Rough Summary
[Java11] Stream Summary -Advantages of Stream-
Try scraping using java [Notes]
Java Stream API cheat sheet
[Implementation] Java Process class notes
Java Collections Framework Review Notes
Java Stream API in 5 minutes
Java8 stream, lambda expression summary
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
Java Stream cannot be reused.
[Java] Introduction to Stream API
Use Redis Stream in Java
[Java11] Stream Usage Summary -Basics-
Java application for beginners: stream
[Java] Stream API intermediate operation
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Stream (filter, map, forEach, reduce)
[java8] To understand the Stream API
[Java] Basic types and instruction notes
About Lambda, Stream, LocalDate of Java8
[Introduction to Java] About Stream API
[Java] Element existence check with Stream
Notes on signal control in Java
Java
I tried using Java8 Stream API
Basic processing flow of java Stream
Notes on Android (java) thread processing
Java
Java 8 ~ Stream API ~ to start now
Notes on Java path and Package
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map
Do you use Stream in Java?
Data processing using stream API from Java 8
Try using the Stream API in Java
Nowadays Java lambda expressions and Stream API
Sort by multiple conditions using Java Stream