Example usage for com.google.common.collect Multimap asMap

List of usage examples for com.google.common.collect Multimap asMap

Introduction

In this page you can find the example usage for com.google.common.collect Multimap asMap.

Prototype

Map<K, Collection<V>> asMap();

Source Link

Document

Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values.

Usage

From source file:org.hupo.psi.mi.example.xml.CreateEntryPerPublication.java

public static void main(String[] args) throws Exception {
    // will read this inputFile
    final PsimiXmlVersion xmlVersion = PsimiXmlVersion.VERSION_254;
    final File inputFile = new File("d:/Downloads/imex-mpidb.xml");
    final File outputFile = new File("d:/Downloads/lala.xml");

    // action!//w ww  . j a  v a  2 s  .c  om

    // We will use a multimap (from the google collections library) to store
    // the interactions grouped by publication id
    Multimap<String, Interaction> publicationMap = HashMultimap.create();

    // Read the file
    PsimiXmlReader reader = new PsimiXmlReader(xmlVersion);
    EntrySet entrySet = reader.read(inputFile);

    // Iterate through the entries
    for (Entry entry : entrySet.getEntries()) {
        for (Interaction interaction : entry.getInteractions()) {
            String publicationId = findPublicationId(interaction);
            publicationMap.put(publicationId, interaction);
        }
    }

    // now create an Entry per interaction
    EntrySet newEntrySet = new EntrySet(xmlVersion);

    // get first source from the original inputFile
    Source source = entrySet.getEntries().iterator().next().getSource();

    // iterating through the multimap, we get the grouped interactions
    for (Map.Entry<String, Collection<Interaction>> pubInteractions : publicationMap.asMap().entrySet()) {
        Entry entry = new Entry(pubInteractions.getValue());
        entry.setSource(source);

        newEntrySet.getEntries().add(entry);
    }

    // write the output file
    PsimiXmlWriter psimiXmlWriter = new PsimiXmlWriter(xmlVersion);
    psimiXmlWriter.write(newEntrySet, outputFile);
}

From source file:com.cinchapi.common.collect.Multimaps.java

/**
 * Return a {@link Map} that contains the data in the {@code multimap}.
 * <p>/*ww w  .j a va 2  s .  co  m*/
 * This method is similar to {@link Multimap#asMap()} except it will flatten
 * a value collection into a single value if the collection only contains
 * one item.
 * </p>
 * 
 * @param multimap
 * @return a {@link Map} with the data in the {@code multimap}
 */
public static <K> Map<K, Object> asMapWithSingleValueWherePossible(Multimap<K, Object> multimap) {
    return multimap.asMap().entrySet().stream().collect(LinkedHashMap::new,
            (map, entry) -> map.put(entry.getKey(), flatten(entry.getValue())), Map::putAll);
}

From source file:org.tensorics.core.iterable.operations.IterableOperations.java

public static <K, V> Map<K, V> reduce(Multimap<K, V> values, IterableOperation<V> operation) {
    return reduce(values.asMap(), operation);
}

From source file:co.freeside.betamax.util.MultimapUtils.java

/**
 * Flattens a `Multimap` whose values are strings into a regular `Map`
 * whose/*from w w  w.  j a  v  a 2 s .  com*/
 * values are comma-separated strings.
 *
 * For example `{"a": ["foo", "bar"], "b": "baz"}` transforms to `{"a":
 * "foo,bar", "b": "baz"}`.
 */
public static Map<String, String> flatten(Multimap<String, String> multimap, String separator) {
    return Maps.transformEntries(multimap.asMap(), new JoinTransformer(separator));
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.params.UrlSolrParams.java

private static Map<String, String[]> createMultiMap(String urlParams) {
    Multimap<String, String> map = getQueryMap(urlParams);

    Map<String, Collection<String>> colMap = map.asMap();

    Map<String, String[]> arrMap = Maps.newHashMapWithExpectedSize(colMap.size());

    for (Map.Entry<String, Collection<String>> entry : colMap.entrySet()) {
        arrMap.put(entry.getKey(), entry.getValue().toArray(new String[entry.getValue().size()]));
    }/*from  w w w. ja v a2  s  .c o  m*/

    return arrMap;
}

From source file:com.twitter.common.collections.Multimaps.java

/**
 * Returns the set of keys associated with groups of a size greater than or equal to a given size.
 *
 * @param map The multimap to search./*from  w  w  w.  jav a2 s.com*/
 * @param minSize The minimum size to return associated keys for.
 * @param <K> The key type for the multimap.
 * @return The keys associated with groups of size greater than or equal to {@code minSize}.
 * @throws IllegalArgumentException if minSize < 0
 */
public static <K> Set<K> getLargeGroups(Multimap<K, ?> map, int minSize) {
    return Sets.newHashSet(Maps.filterValues(map.asMap(), new AtLeastSize(minSize)).keySet());
}

From source file:org.apache.storm.metric.FakeMetricConsumer.java

public static Map<Integer, Collection<Object>> getTaskIdToBuckets(String componentName, String metricName) {
    synchronized (buffer) {
        Multimap<Integer, Object> taskIdToBuckets = buffer.get(componentName, metricName);
        return (null != taskIdToBuckets) ? taskIdToBuckets.asMap() : null;
    }/* w ww .j  av a  2  s .  com*/
}

From source file:de.metas.ui.web.order.sales.purchasePlanning.process.WEBUI_SalesOrder_Apply_Availability_Row.java

private static boolean hasMultipleAvailabilityRowsPerLineRow(
        @NonNull final Multimap<PurchaseRow, PurchaseRow> lineRows2availabilityRows) {
    return lineRows2availabilityRows.asMap().entrySet().stream()
            .anyMatch(lineRow2availabilityRows -> lineRow2availabilityRows.getValue().size() > 1);
}

From source file:springfox.documentation.spring.web.plugins.DuplicateGroupsDetector.java

public static void ensureNoDuplicateGroups(List<DocumentationPlugin> allPlugins) throws IllegalStateException {
    Multimap<String, DocumentationPlugin> plugins = Multimaps.index(allPlugins, byGroupName());
    Iterable<String> duplicateGroups = from(plugins.asMap().entrySet()).filter(duplicates())
            .transform(toGroupNames());/* w  ww .j ava  2s. com*/
    if (Iterables.size(duplicateGroups) > 0) {
        throw new IllegalStateException(String.format(
                "Multiple Dockets with the same group name are not supported. "
                        + "The following duplicate groups were discovered. %s",
                Joiner.on(',').join(duplicateGroups)));
    }
}

From source file:org.sonar.ce.taskprocessor.CeTaskProcessorRepositoryImpl.java

private static void checkUniqueHandlerPerCeTaskType(Multimap<String, CeTaskProcessor> permissiveIndex) {
    for (Map.Entry<String, Collection<CeTaskProcessor>> entry : permissiveIndex.asMap().entrySet()) {
        checkArgument(entry.getValue().size() == 1, format(
                "There can be only one CeTaskProcessor instance registered as the processor for CeTask type %s. "
                        + "More than one found. Please fix your configuration: %s",
                entry.getKey(), COMMA_JOINER.join(from(entry.getValue()).transform(ToClassName.INSTANCE)
                        .toSortedList(CASE_INSENSITIVE_ORDER))));
    }//  w w w .  j av  a 2  s. co  m
}