Advanced stream operations for key-value data processing with the MapStream interface providing specialized operations for Map-like data structures. This extends Java's Stream API with dedicated support for key-value pair operations, enabling powerful data transformation pipelines.
Specialized stream interface for processing key-value pairs with dedicated operations for keys, values, and entries.
/**
* Stream-like operations on key-value pairs
*/
public interface MapStream<K, V> extends BaseStream<Entry<K, V>, MapStream<K, V>> {
/**
* Create MapStream from Map
* @param map Source map
* @return MapStream of map entries
*/
static <K, V> MapStream<K, V> of(Map<? extends K, ? extends V> map);
/**
* Create MapStream from nullable Map
* @param map Source map (can be null)
* @return MapStream of map entries or empty stream
*/
static <K, V> MapStream<K, V> ofNullable(Map<? extends K, ? extends V> map);
/**
* Create MapStream from Entry collection
* @param collection Collection of entries
* @return MapStream of entries
*/
static <K, V> MapStream<K, V> of(Collection<? extends Entry<? extends K, ? extends V>> collection);
/**
* Create MapStream from Entry stream
* @param stream Stream of entries
* @return MapStream of entries
*/
static <K, V> MapStream<K, V> of(Stream<? extends Entry<? extends K, ? extends V>> stream);
/**
* Concatenate two MapStreams
* @param a First MapStream
* @param b Second MapStream
* @return Concatenated MapStream
*/
static <K, V> MapStream<K, V> concat(MapStream<? extends K, ? extends V> a, MapStream<? extends K, ? extends V> b);
/**
* Create empty MapStream
* @return Empty MapStream
*/
static <K, V> MapStream<K, V> empty();
/**
* Create MapStream from individual key-value pairs
* @param k1 First key
* @param v1 First value
* @return MapStream with single entry
*/
static <K, V> MapStream<K, V> of(K k1, V v1);
/**
* Create MapStream from key-value pairs (up to 10 pairs supported)
*/
static <K, V> MapStream<K, V> of(K k1, V v1, K k2, V v2);
// ... additional overloads for 3-10 key-value pairs
/**
* Create MapStream from Entry varargs
* @param entries Entries to include
* @return MapStream of entries
*/
static <K, V> MapStream<K, V> ofEntries(Entry<? extends K, ? extends V>... entries);
/**
* Create Entry instance
* @param key Entry key
* @param value Entry value
* @return Map Entry instance
*/
static <K, V> Entry<K, V> entry(K key, V value);
/**
* Get stream of entries
* @return Stream of Map.Entry objects
*/
Stream<Entry<K, V>> entries();
/**
* Get stream of keys
* @return Stream of keys
*/
Stream<K> keys();
/**
* Get stream of values
* @return Stream of values
*/
Stream<V> values();
/**
* Filter entries by predicate
* @param predicate Entry filter predicate
* @return Filtered MapStream
*/
MapStream<K, V> filter(Predicate<? super Entry<K, V>> predicate);
/**
* Filter entries by key predicate
* @param predicate Key filter predicate
* @return Filtered MapStream
*/
MapStream<K, V> filterKey(Predicate<? super K> predicate);
/**
* Filter entries by value predicate
* @param predicate Value filter predicate
* @return Filtered MapStream
*/
MapStream<K, V> filterValue(Predicate<? super V> predicate);
/**
* Transform entries
* @param mapper Entry transformation function
* @return MapStream with transformed entries
*/
<R, S> MapStream<R, S> map(Function<? super Entry<K, V>, ? extends Entry<R, S>> mapper);
/**
* Transform keys
* @param mapper Key transformation function
* @return MapStream with transformed keys
*/
<R> MapStream<R, V> mapKey(Function<? super K, ? extends R> mapper);
/**
* Transform values
* @param mapper Value transformation function
* @return MapStream with transformed values
*/
<R> MapStream<K, R> mapValue(Function<? super V, ? extends R> mapper);
/**
* Transform to object stream
* @param mapper Entry to object transformation function
* @return Stream of transformed objects
*/
<R> Stream<R> mapToObj(Function<? super Entry<K, V>, ? extends R> mapper);
/**
* Transform to int stream
* @param mapper Entry to int transformation function
* @return IntStream of transformed values
*/
IntStream mapToInt(ToIntFunction<? super Entry<K, V>> mapper);
/**
* Transform to long stream
* @param mapper Entry to long transformation function
* @return LongStream of transformed values
*/
LongStream mapToLong(ToLongFunction<? super Entry<K, V>> mapper);
/**
* Transform to double stream
* @param mapper Entry to double transformation function
* @return DoubleStream of transformed values
*/
DoubleStream mapToDouble(ToDoubleFunction<? super Entry<K, V>> mapper);
/**
* Flat map entries to multiple entries
* @param mapper Function producing stream of entries
* @return Flattened MapStream
*/
<R, S> MapStream<R, S> flatMap(Function<? super Entry<K, V>, ? extends Stream<? extends Entry<R, S>>> mapper);
/**
* Flat map keys to multiple entries
* @param mapper Function producing stream of entries from key
* @return Flattened MapStream
*/
<R, S> MapStream<R, S> flatMapKey(Function<? super K, ? extends Stream<? extends Entry<R, S>>> mapper);
/**
* Flat map values to multiple entries
* @param mapper Function producing stream of entries from value
* @return Flattened MapStream
*/
<R, S> MapStream<R, S> flatMapValue(Function<? super V, ? extends Stream<? extends Entry<R, S>>> mapper);
/**
* Peek at entries without modifying stream
* @param action Action to perform on each entry
* @return This MapStream
*/
MapStream<K, V> peek(Consumer<? super Entry<K, V>> action);
/**
* Peek at keys without modifying stream
* @param action Action to perform on each key
* @return This MapStream
*/
MapStream<K, V> peekKey(Consumer<? super K> action);
/**
* Peek at values without modifying stream
* @param action Action to perform on each value
* @return This MapStream
*/
MapStream<K, V> peekValue(Consumer<? super V> action);
/**
* Remove duplicate entries
* @return MapStream with unique entries
*/
MapStream<K, V> distinct();
/**
* Sort entries by natural order
* @return Sorted MapStream
*/
MapStream<K, V> sorted();
/**
* Sort entries by comparator
* @param comparator Entry comparator
* @return Sorted MapStream
*/
MapStream<K, V> sorted(Comparator<? super Entry<K, V>> comparator);
/**
* Sort entries by key comparator
* @param comparator Key comparator
* @return Sorted MapStream
*/
MapStream<K, V> sortedByKey(Comparator<? super K> comparator);
/**
* Sort entries by value comparator
* @param comparator Value comparator
* @return Sorted MapStream
*/
MapStream<K, V> sortedByValue(Comparator<? super V> comparator);
/**
* Limit stream to first n entries
* @param maxSize Maximum number of entries
* @return Limited MapStream
*/
MapStream<K, V> limit(long maxSize);
/**
* Skip first n entries
* @param n Number of entries to skip
* @return MapStream with skipped entries
*/
MapStream<K, V> skip(long n);
/**
* Count entries in stream
* @return Number of entries
*/
long count();
/**
* Apply action to each entry
* @param action Action to perform
*/
void forEach(Consumer<? super Entry<K, V>> action);
/**
* Apply action to each entry in encounter order
* @param action Action to perform
*/
void forEachOrdered(Consumer<? super Entry<K, V>> action);
/**
* Check if any entry matches predicate
* @param predicate Test predicate
* @return true if any match found
*/
boolean anyMatch(Predicate<? super Entry<K, V>> predicate);
/**
* Check if all entries match predicate
* @param predicate Test predicate
* @return true if all match
*/
boolean allMatch(Predicate<? super Entry<K, V>> predicate);
/**
* Check if no entries match predicate
* @param predicate Test predicate
* @return true if none match
*/
boolean noneMatch(Predicate<? super Entry<K, V>> predicate);
/**
* Find any entry
* @return Optional containing any entry or empty
*/
Optional<Entry<K, V>> findAny();
/**
* Find first entry
* @return Optional containing first entry or empty
*/
Optional<Entry<K, V>> findFirst();
/**
* Collect entries using collector
* @param collector Collector to use
* @return Collected result
*/
<R> R collect(Collector<? super Entry<K, V>, ?, R> collector);
/**
* Collect to Map
* @return Map containing all entries
*/
Map<K, V> collect(Collector<Entry<K, V>, ?, Map<K, V>> collector);
/**
* Convert to array
* @return Array of entries
*/
Object[] toArray();
/**
* Convert to typed array
* @param generator Array generator function
* @return Typed array of entries
*/
<A> A[] toArray(IntFunction<A[]> generator);
}Usage Examples:
import aQute.bnd.stream.MapStream;
import java.util.Map;
import java.util.stream.Collectors;
// Create from Map
Map<String, Integer> data = Map.of("a", 1, "b", 2, "c", 3);
MapStream<String, Integer> stream = MapStream.of(data);
// Filter and transform
Map<String, String> result = MapStream.of(data)
.filterValue(v -> v > 1)
.mapValue(v -> "Value: " + v)
.collect(Collectors.toMap(
Entry::getKey,
Entry::getValue
));
// Key-based operations
MapStream.of("apple", 5, "banana", 6, "cherry", 6)
.filterKey(k -> k.startsWith("a"))
.mapValue(v -> v * 2)
.forEach(entry -> System.out.println(entry));
// Complex transformations
Map<String, List<String>> grouped = MapStream.of(
"apple", "fruit",
"carrot", "vegetable",
"banana", "fruit"
)
.collect(Collectors.groupingBy(
Entry::getValue,
Collectors.mapping(Entry::getKey, Collectors.toList())
));
// Stream operations
MapStream.of(data)
.peekKey(k -> System.out.println("Processing: " + k))
.sortedByValue(Integer::compareTo)
.limit(2)
.mapToObj(entry -> entry.getKey() + "=" + entry.getValue())
.forEach(System.out::println);Functional interface for consuming three arguments, useful for complex stream operations.
/**
* Functional interface for consuming three arguments
*/
@FunctionalInterface
public interface TriConsumer<T, U, V> {
/**
* Accept three arguments
* @param t First argument
* @param u Second argument
* @param v Third argument
*/
void accept(T t, U u, V v);
}Usage Examples:
import aQute.bnd.stream.TriConsumer;
// Use with complex operations
TriConsumer<String, Integer, Boolean> processor = (name, count, enabled) -> {
if (enabled) {
System.out.println("Processing " + name + " with count " + count);
}
};
// Apply the consumer
processor.accept("test", 42, true);
// Use in stream operations or method references
someDataStructure.forEach(processor);Specialized collectors for MapStream operations.
public interface MapStream<K, V> {
/**
* Collector for creating immutable Map
* @return Collector that produces Map
*/
static <K, V> Collector<Entry<K, V>, ?, Map<K, V>> toMap();
/**
* Collector for creating Map with merge function
* @param mergeFunction Function to merge duplicate values
* @return Collector that produces Map
*/
static <K, V> Collector<Entry<K, V>, ?, Map<K, V>> toMap(BinaryOperator<V> mergeFunction);
/**
* Collector for creating specific Map type
* @param mergeFunction Function to merge duplicate values
* @param mapSupplier Supplier for Map implementation
* @return Collector that produces specified Map type
*/
static <K, V, M extends Map<K, V>> Collector<Entry<K, V>, ?, M> toMap(
BinaryOperator<V> mergeFunction,
Supplier<M> mapSupplier);
}