amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 249 forks source link

Provide factory method zip(List<K> keys, V constant) in EntryStream #271

Closed mizhoux closed 10 months ago

mizhoux commented 10 months ago

Currently, if we want to zip keys with a single value to get an EntryStream, we need to do this:

void demo(List<Long> userIds, Config config) {
    // EntryStream<Long, Config>
    StreamEx.of(userIds).zipWith(StreamEx.constant(config, userIds.size()));
}

If EntryStream had a factory method like this:

public static <K, V> EntryStream<K, V> zip(List<K> keys, V constant) {
    // Maybe we can implement it more efficiently than:
    // StreamEx.of(keys).zipWith(StreamEx.constant(constant, keys.size()))
}

Then it's much easier (and efficient) to use:

void demo(List<Long> userIds, Config config) {
    // EntryStream<Long, Config>
    EntryStream.zip(userIds, config);
}
amaembo commented 10 months ago

Hello!

In this case, you don't need zip. It's better to use mapToEntry

StreamEx.of(userIds).mapToEntry(id -> config)

This is essentially not a zipping, it's a plain mapping of a single element to a pair.

mizhoux commented 10 months ago

Wow, thanks for helping me find the method.