From ff434cb285d966cb33808b60a8a8716e9281777c Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 2 Oct 2019 11:27:23 +0200 Subject: [PATCH] [New] Added SearchResult.toMapOf* --- .../java/li/strolch/search/SearchResult.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java b/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java index a01edf522..6e9e89703 100644 --- a/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java +++ b/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java @@ -10,6 +10,9 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; +import li.strolch.utils.collections.MapOfLists; +import li.strolch.utils.collections.MapOfMaps; +import li.strolch.utils.collections.MapOfSets; import li.strolch.utils.collections.Paging; /** @@ -119,6 +122,102 @@ public class SearchResult { return this.stream.collect(Collectors.toMap(keyMapper, valueMapper)); } + /** + * Collects this stream to a {@link MapOfSets}, using the given key mapper. The value is returned as is + * + * @param keyMapper + * function to get the key of the element + * + * @return a map of this stream + */ + public MapOfSets toMapOfSets(Function keyMapper) { + return this.stream.collect(MapOfSets::new, // + (map, e) -> map.addElement(keyMapper.apply(e), e), // + MapOfSets::addAll); + } + + /** + * Collects this stream to a {@link MapOfSets}, using the given key mapper + * + * @param keyMapper + * function to get the key of the element + * @param valueMapper + * function to get the value of the element + * + * @return a map of this stream + */ + public MapOfSets toMapOfSets(Function keyMapper, Function valueMapper) { + return this.stream.collect(MapOfSets::new, // + (map, e) -> map.addElement(keyMapper.apply(e), valueMapper.apply(e)), // + MapOfSets::addAll); + } + + /** + * Collects this stream to a {@link MapOfLists}, using the given key mapper. The value is returned as is + * + * @param keyMapper + * function to get the key of the element + * + * @return a map of this stream + */ + public MapOfLists toMapOfLists(Function keyMapper) { + return this.stream.collect(MapOfLists::new, // + (map, e) -> map.addElement(keyMapper.apply(e), e), // + MapOfLists::addAll); + } + + /** + * Collects this stream to a {@link MapOfLists}, using the given key mapper + * + * @param keyMapper + * function to get the key of the element + * @param valueMapper + * function to get the value of the element + * + * @return a map of this stream + */ + public MapOfLists toMapOfLists(Function keyMapper, Function valueMapper) { + return this.stream.collect(MapOfLists::new, // + (map, e) -> map.addElement(keyMapper.apply(e), valueMapper.apply(e)), // + MapOfLists::addAll); + } + + /** + * Collects this stream to a {@link MapOfMaps}, using the given key mapper and sub key mapper. The value is returned + * as is + * + * @param keyMapper + * function to get the key of the element + * @param subKeyMapper + * function to get the sub key of the element + * + * @return a map of this stream + */ + public MapOfMaps toMapOfMaps(Function keyMapper, Function subKeyMapper) { + return this.stream.collect(MapOfMaps::new, // + (map, e) -> map.addElement(keyMapper.apply(e), subKeyMapper.apply(e), e), // + MapOfMaps::putAll); + } + + /** + * Collects this stream to a {@link MapOfLists}, using the given key mapper + * + * @param keyMapper + * function to get the key of the element + * @param subKeyMapper + * function to get the sub key of the element + * @param valueMapper + * function to get the value of the element + * + * @return a map of this stream + */ + public MapOfMaps toMapOfMaps(Function keyMapper, Function subKeyMapper, + Function valueMapper) { + return this.stream.collect(MapOfMaps::new, // + (map, e) -> map.addElement(keyMapper.apply(e), subKeyMapper.apply(e), valueMapper.apply(e)), // + MapOfMaps::putAll); + } + /** * Returns a {@link Paging} element to use this object in paged results *