import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.stream.Collector; import java.util.stream.Collectors; class MyTest { public static CompletableFuture mapCollect(Iterator iterator, Function> body, Collector collector) { throw new RuntimeException(); } public static void main(String[] args) throws Exception { Collection> test = new ArrayList<>(); final CompletableFuture> future = mapCollect( test.iterator(), e -> CompletableFuture.completedFuture(new Pair(e.getKey(), e.getValue())), Collectors.toMap( e -> e.getKey(), e -> e.getValue() ) ); future.get(); } public static void main2(String[] args) throws Exception { Collection> test = new ArrayList<>(); final CompletableFuture> future = mapCollect( test.iterator(), e -> CompletableFuture.completedFuture(Pair.of(e.getKey(), e.getValue())), Collectors.toMap( e -> e.getKey(), e -> e.getValue() ) ); future.get(); } public static class Pair { private final K key; private final V value; public Pair(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } public static Pair of(A a, B b) { return new Pair(a, b); } } }