/** * Creates an index {@code ImmutableListMultimap} that contains the results of applying a * specified function to each item in this {@code FluentIterable} of values. Each element of this * iterable will be stored as a value in the resulting multimap, yielding a multimap with the same * size as this iterable. The key used to store that value in the multimap will be the result of * calling the function on that value. The resulting multimap is created as an immutable snapshot. * In the returned multimap, keys appear in the order they are first encountered, and the values * corresponding to each key appear in the same order as they are encountered. * * <p><b>{@code Stream} equivalent:</b> {@code stream.collect(Collectors.groupingBy(keyFunction))} * behaves similarly, but returns a mutable {@code Map<K, List<E>>} instead, and may not preserve * the order of entries). * * @param keyFunction the function used to produce the key for each value * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code * keyFunction} produces {@code null} for any key * @since 14.0 */ public final <K> ImmutableListMultimap<K, E> index(Function<? super E, K> keyFunction) { return Multimaps.index(getDelegate(), keyFunction); }
return index(values.iterator(), keyFunction);
/** * Creates an index {@code ImmutableListMultimap} that contains the results of applying a * specified function to each item in this {@code FluentIterable} of values. Each element of this * iterable will be stored as a value in the resulting multimap, yielding a multimap with the same * size as this iterable. The key used to store that value in the multimap will be the result of * calling the function on that value. The resulting multimap is created as an immutable snapshot. * In the returned multimap, keys appear in the order they are first encountered, and the values * corresponding to each key appear in the same order as they are encountered. * * <p><b>{@code Stream} equivalent:</b> {@code stream.collect(Collectors.groupingBy(keyFunction))} * behaves similarly, but returns a mutable {@code Map<K, List<E>>} instead, and may not preserve * the order of entries). * * @param keyFunction the function used to produce the key for each value * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code * keyFunction} produces {@code null} for any key * @since 14.0 */ public final <K> ImmutableListMultimap<K, E> index(Function<? super E, K> keyFunction) { return Multimaps.index(getDelegate(), keyFunction); }
/** * Creates an index {@code ImmutableListMultimap} that contains the results of applying a * specified function to each item in this {@code FluentIterable} of values. Each element of this * iterable will be stored as a value in the resulting multimap, yielding a multimap with the same * size as this iterable. The key used to store that value in the multimap will be the result of * calling the function on that value. The resulting multimap is created as an immutable snapshot. * In the returned multimap, keys appear in the order they are first encountered, and the values * corresponding to each key appear in the same order as they are encountered. * * <p><b>{@code Stream} equivalent:</b> {@code stream.collect(Collectors.groupingBy(keyFunction))} * behaves similarly, but returns a mutable {@code Map<K, List<E>>} instead, and may not preserve * the order of entries). * * @param keyFunction the function used to produce the key for each value * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code * keyFunction} produces {@code null} for any key * @since 14.0 */ public final <K> ImmutableListMultimap<K, E> index(Function<? super E, K> keyFunction) { return Multimaps.index(getDelegate(), keyFunction); }
public void publishSegments(Iterable<DataSegment> segments) throws IOException { // Request segment pushes for each set final Multimap<Interval, DataSegment> segmentMultimap = Multimaps.index( segments, new Function<DataSegment, Interval>() { @Override public Interval apply(DataSegment segment) { return segment.getInterval(); } } ); for (final Collection<DataSegment> segmentCollection : segmentMultimap.asMap().values()) { getTaskActionClient().submit(new SegmentInsertAction(ImmutableSet.copyOf(segmentCollection))); } }
public FunctionMap(FunctionMap map, Iterable<? extends SqlFunction> functions) { this.functions = ImmutableListMultimap.<QualifiedName, SqlFunction>builder() .putAll(map.functions) .putAll(Multimaps.index(functions, function -> QualifiedName.of(function.getSignature().getName()))) .build(); // Make sure all functions with the same name are aggregations or none of them are for (Map.Entry<QualifiedName, Collection<SqlFunction>> entry : this.functions.asMap().entrySet()) { Collection<SqlFunction> values = entry.getValue(); long aggregations = values.stream() .map(function -> function.getSignature().getKind()) .filter(kind -> kind == AGGREGATE) .count(); checkState(aggregations == 0 || aggregations == values.size(), "'%s' is both an aggregation and a scalar function", entry.getKey()); } }
public void testIndexIterator() { final Multimap<String, Object> stringToObject = new ImmutableMultimap.Builder<String, Object>() .put("1", 1) .put("1", 1L) .put("1", "1") .put("2", 2) .put("2", 2L) .build(); ImmutableMultimap<String, Object> outputMap = Multimaps.index(stringToObject.values().iterator(), Functions.toStringFunction()); assertEquals(stringToObject, outputMap); }
public void testIndex() { final Multimap<String, Object> stringToObject = new ImmutableMultimap.Builder<String, Object>() .put("1", 1) .put("1", 1L) .put("1", "1") .put("2", 2) .put("2", 2L) .build(); ImmutableMultimap<String, Object> outputMap = Multimaps.index(stringToObject.values(), Functions.toStringFunction()); assertEquals(stringToObject, outputMap); }
public void testIndex_nullKey() { List<Integer> values = Arrays.asList(1, 2); try { Multimaps.index(values, Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
public void testIndex_nullValue() { List<Integer> values = Arrays.asList(1, null); try { Multimaps.index(values, Functions.identity()); fail(); } catch (NullPointerException expected) { } }
LongPositions(Iterable<? extends Object> elements, final int bitPerLong) { this.elements = ImmutableList.copyOf(elements); checkArgument(bitPerLong <= BITS_IN_LONG, bitPerLong); for (int i = 0; i < this.elements.size(); i++) { positions.put( this.elements.get(i), new BitPosition( i / bitPerLong, i % bitPerLong)); } this.longPositions = ImmutableSortedMap.copyOf( Maps.transformEntries( Multimaps.index(positions.values(), ToLongIndex.FUNCTION).asMap(), new Maps.EntryTransformer<Integer, Collection<BitPosition>, LongSet>() { @Override public LongSet transformEntry(Integer key, Collection<BitPosition> position) { return new LongSet(key, position); } })); }
@Benchmark public ImmutableListMultimap<Integer, Integer> groupBy_unordered_lists_100_keys_serial_eager_guava() { ImmutableListMultimap<Integer, Integer> multimap = Multimaps.index(this.integersJDK, each -> each % 100); Verify.assertSize(100, multimap.asMap()); return multimap; }
@Benchmark public void groupBy_2_keys_serial_eager_guava() { Verify.assertSize(2, Multimaps.index(this.integersJDK, each -> each % 2 == 0).asMap()); }
@Benchmark public ImmutableListMultimap<Boolean, Integer> groupBy_unordered_lists_2_keys_serial_eager_guava() { ImmutableListMultimap<Boolean, Integer> multimap = Multimaps.index(this.integersJDK, each -> each % 2 == 0); Verify.assertSize(2, multimap.asMap()); return multimap; }
@Benchmark public void groupBy_100_keys_serial_eager_guava() { Verify.assertSize(100, Multimaps.index(this.integersJDK, each -> each % 100).asMap()); }
private static final class TransformedEntriesListMultimap<K, V1, V2> extends TransformedEntriesMultimap<K, V1, V2> implements ListMultimap<K, V2> { TransformedEntriesListMultimap( ListMultimap<K, V1> fromMultimap, EntryTransformer<? super K, ? super V1, V2> transformer) { super(fromMultimap, transformer); } @Override List<V2> transform(K key, Collection<V1> values) { return Lists.transform((List<V1>) values, Maps.asValueToValueFunction(transformer, key)); } @Override public List<V2> get(K key) { return transform(key, fromMultimap.get(key)); } @SuppressWarnings("unchecked") @Override public List<V2> removeAll(Object key) { return transform((K) key, fromMultimap.removeAll(key)); } @Override public List<V2> replaceValues(K key, Iterable<? extends V2> values) { throw new UnsupportedOperationException(); } }
/** * Sends as many emails as there are unique combinations of: * * [mail creator] x [failure email address list] * * Executions with the same combo are grouped into a single message. */ @Override public void alertOnFailedUpdate(final Executor executor, List<ExecutableFlow> flows, final ExecutorManagerException updateException) { flows = flows.stream() .filter(flow -> flow.getExecutionOptions() != null) .filter(flow -> CollectionUtils.isNotEmpty(flow.getExecutionOptions().getFailureEmails())) .collect(Collectors.toList()); // group by mail creator in case some flows use different creators final ImmutableListMultimap<String, ExecutableFlow> creatorsToFlows = Multimaps .index(flows, flow -> flow.getExecutionOptions().getMailCreator()); for (final String mailCreatorName : creatorsToFlows.keySet()) { final ImmutableList<ExecutableFlow> creatorFlows = creatorsToFlows.get(mailCreatorName); final MailCreator mailCreator = getMailCreator(mailCreatorName); // group by recipients in case some flows have different failure email addresses final ImmutableListMultimap<List<String>, ExecutableFlow> emailsToFlows = Multimaps .index(creatorFlows, flow -> flow.getExecutionOptions().getFailureEmails()); for (final List<String> emailList : emailsToFlows.keySet()) { sendFailedUpdateEmail(executor, updateException, mailCreator, emailsToFlows.get(emailList)); } } }
public static List<PartitionUpdate> mergePartitionUpdates(Iterable<PartitionUpdate> unMergedUpdates) for (Collection<PartitionUpdate> partitionGroup : Multimaps.index(unMergedUpdates, PartitionUpdate::getName).asMap().values()) { PartitionUpdate firstPartition = partitionGroup.iterator().next();
public void testIndex_ordering() { final Multimap<Integer, String> expectedIndex = new ImmutableListMultimap.Builder<Integer, String>() .put(4, "Inky") .put(6, "Blinky") .put(5, "Pinky") .put(5, "Pinky") .put(5, "Clyde") .build(); final List<String> badGuys = Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde"); final Function<String, Integer> stringLengthFunction = new Function<String, Integer>() { @Override public Integer apply(String input) { return input.length(); } }; Multimap<Integer, String> index = Multimaps.index(badGuys, stringLengthFunction); assertEquals(expectedIndex, index); }
ImmutableMultimap<String, MaterializedRow> functions = Multimaps.index(result.getMaterializedRows(), input -> { assertEquals(input.getFieldCount(), 6); return (String) input.getField(0);