/** * Accumulates all key-value pairs of this {@code MapStream} into a * {@code ConcurrentMap}. * <p> * If the mapped keys contains duplicates (according to * {@link Object#equals(Object)}), an {@code IllegalStateException} is * thrown when the collection operation is performed. If the mapped keys * may have duplicates, use {@link #toConcurrentMap(BinaryOperator)} instead. * * <p> * It is common for either the key or the value to be the input elements. * In this case, the utility method * {@link java.util.function.Function#identity()} may be helpful. * <p> * This is a {@link Collector.Characteristics#CONCURRENT concurrent} and * {@link Collector.Characteristics#UNORDERED unordered} Collector. * * @return a {@code ConcurrentMap} whose keys are taken from this stream * * @see #toMap() * @see #toConcurrentMap(BinaryOperator) */ public Map<K, V> toConcurrentMap() { return inner.collect(Collectors.toConcurrentMap( Map.Entry::getKey, Map.Entry::getValue )); }
/** * Accumulates all key-value pairs of this {@code MapStream} into a * {@code ConcurrentMap}. * <p> * If the mapped keys contains duplicates (according to * {@link Object#equals(Object)}), the value mapping function is applied to * each equal element, and the results are merged using the provided merging * function. * * <p> * There are multiple ways to deal with collisions between multiple elements * mapping to the same key. The other forms of {@code toConcurrentMap} simply use * a merge function that throws unconditionally, but you can easily write * more flexible merge policies. * * @param mergeFunction a merge function, used to resolve collisions between * values associated with the same key, as supplied * to {@link Map#merge(Object, Object, BiFunction)} * @return a {@code ConcurrentMap} whose keys are taken from this stream * * @see #toConcurrentMap() * @see #toMap(BinaryOperator) */ public Map<K, V> toConcurrentMap(BinaryOperator<V> mergeFunction) { return inner.collect(Collectors.toConcurrentMap( Map.Entry::getKey, Map.Entry::getValue, mergeFunction )); }
return inner.collect(Collectors.toConcurrentMap( Map.Entry::getKey, Map.Entry::getValue,
/** * Creates a new instance * @param users the {@link UserDetails} to use */ public MapReactiveUserDetailsService(Collection<UserDetails> users) { Assert.notEmpty(users, "users cannot be null or empty"); this.users = users.stream().collect(Collectors.toConcurrentMap( u -> getKey(u.getUsername()), Function.identity())); }
return inner.collect(Collectors.toConcurrentMap( Map.Entry::getKey, Map.Entry::getValue,
/** * Creates a new instance * @param users the {@link UserDetails} to use */ public MapReactiveUserDetailsService(Collection<UserDetails> users) { Assert.notEmpty(users, "users cannot be null or empty"); this.users = users.stream().collect(Collectors.toConcurrentMap( u -> getKey(u.getUsername()), Function.identity())); }
/** * Accumulates all key-value pairs of this {@code MapStream} into a * {@code ConcurrentNavigableMap}, sorting the entries by applying the * specified comparator to the keys of the stream. * <p> * If the mapped keys contains duplicates (according to * {@link Object#equals(Object)}), an {@code IllegalStateException} is * thrown when the collection operation is performed. If the mapped keys * may have duplicates, use {@link #toConcurrentNavigableMap(Comparator, BinaryOperator)} * instead. * * @param keyComparator the comparator to use when sorting the keys * @return a {@code ConcurrentNavigableMap} whose keys and * values are identical to the entries of this stream * * @see #toMap(BinaryOperator) * @see #toConcurrentMap() * @see #toSortedMap() */ public ConcurrentNavigableMap<K, V> toConcurrentNavigableMapByKey(Comparator<K> keyComparator) { return inner.collect(Collectors.toConcurrentMap( Map.Entry::getKey, Map.Entry::getValue, throwingMerger(), () -> new ConcurrentSkipListMap<>(keyComparator) )); }
/** * Accumulates all key-value pairs of this {@code MapStream} into a * {@code ConcurrentNavigableMap}. * <p> * If the mapped keys contains duplicates (according to * {@link Object#equals(Object)}), an {@code IllegalStateException} is * thrown when the collection operation is performed. If the mapped keys * may have duplicates, use {@link #toConcurrentNavigableMap(BinaryOperator)} * instead. * <p> * If the keys are not {@link Comparable}, a * {@code java.lang.ClassCastException} may be thrown when the terminal * operation is executed. It is therefore better to use * {@link #toConcurrentNavigableMapByKey(Comparator)} since it allows you to specify * a comparator to use when comparing the keys. * * @return a {@code ConcurrentNavigableMap} whose keys and values are * identical to the entries of this stream * * @see #toMap(BinaryOperator) * @see #toConcurrentMap() */ public ConcurrentNavigableMap<K, V> toConcurrentNavigableMap() { return inner.collect(Collectors.toConcurrentMap( Map.Entry::getKey, Map.Entry::getValue, throwingMerger(), ConcurrentSkipListMap::new )); }
/** * Builds a new code area with the given enum type as layer id provider. * Constants of the enum will identify layers of the code area. * * @param idEnum Enum type */ // the annotation lets the value be passed from FXML public HighlightLayerCodeArea(@NamedArg("idEnum") Class<K> idEnum) { super(); this.layersById = EnumSet.allOf(idEnum) .stream() .collect(Collectors.toConcurrentMap(id -> id, id -> new StyleLayer())); }
.collect(Collectors.toConcurrentMap(UserKey::new, identity())); } catch (Exception e) { log.error("Error reading user profiles from disk. {}", e.getMessage());
public void cacheRacks(List<SingularityRack> racks) { this.racks = racks.stream().collect(Collectors.toConcurrentMap(SingularityRack::getId, Function.identity())); } public void stop() {
public void cacheSlaves(List<SingularitySlave> slaves) { this.slaves = slaves.stream().collect(Collectors.toConcurrentMap(SingularitySlave::getId, Function.identity())); }
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ParallelWordCount { public static void main(String[] args) { List<String> list = Arrays.asList( "hello", "bye", "ciao", "bye", "ciao"); Map<String, Integer> counts = list.parallelStream(). collect(Collectors.toConcurrentMap( w -> w, w -> 1, Integer::sum)); System.out.println(counts); } }
Collectors.toConcurrentMap( w -> w, w -> 1, Integer::sum)); bh.consume(counts);
/** * @param s stream to turn to map. The stream is consumed by this operation. * @param key function to create the key * @param val function to create the value * @param <K> type of key * @param <V> type of value * @param <Q> type of input * @return map including all elements in the stream, keyed by key and valued by val */ public static <K, V, Q> Map<K, V> asMap(Stream<Q> s, Function<Q, K> key, Function<Q, V> val) { return s.collect(Collectors.toConcurrentMap(key::apply, val::apply)); }
/** * @param s stream to turn to map. The stream is consumed by this operation. * @param key function to create the key * @param val function to create the value * @param <K> type of key * @param <V> type of value * @param <Q> type of input * @return map including all elements in the stream, keyed by key and valued by val */ public static <K, V, Q> Map<K, V> asMap(Stream<Q> s, Function<Q, K> key, Function<Q, V> val) { return s.collect(Collectors.toConcurrentMap(key::apply, val::apply)); }
/** * @param s stream to turn to map. The stream is consumed by this operation. * @param key function to create the key * @param val function to create the value * @param <K> type of key * @param <V> type of value * @param <Q> type of input * @return map including all elements in the stream, keyed by key and valued by val */ public static <K, V, Q> Map<K, V> asMap(Stream<Q> s, Function<Q, K> key, Function<Q, V> val) { return s.collect(Collectors.toConcurrentMap(key::apply, val::apply)); }
@Override public RepositoryDTO fetchInstallableApplications() { LOGGER.info(String.format("Fetching applications for: %s", this.toString())); /* * This step is needed because we need a mapping between the CategoryDTO * list and its application source, to preserve the order in the * reduction step */ final Map<Repository, RepositoryDTO> repositoriesMap = this.repositories.stream().parallel() .collect(Collectors.toConcurrentMap(source -> source, Repository::fetchInstallableApplications)); return mergeRepositories(repositoriesMap, repositories); }
@SuppressWarnings("unchecked") private Map<String, byte[]> loadResources() { return Arrays.stream(RESOURCE_NAMES) .map(resourceName -> new Tuple<>(resourceName, loadResource(resourceName))) .collect(Collectors.toConcurrentMap(Tuple::getLeft, Tuple::getRight)); }
@SuppressWarnings("unchecked") private Map<String, byte[]> loadResources() { return Arrays.stream(RESOURCE_NAMES) .map(resourceName -> new Tuple<>(resourceName, loadResource(resourceName))) .collect(Collectors.toConcurrentMap(Tuple::getLeft, Tuple::getRight)); }