Example usage for org.apache.commons.collections4 MapUtils invertMap

List of usage examples for org.apache.commons.collections4 MapUtils invertMap

Introduction

In this page you can find the example usage for org.apache.commons.collections4 MapUtils invertMap.

Prototype

public static <K, V> Map<V, K> invertMap(final Map<K, V> map) 

Source Link

Document

Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values.

Usage

From source file:com.sunchenbin.store.feilong.core.util.MapUtil.java

/**
 * mapkeyvalue./*from   w  ww  . j a  va2 s  .c  o m*/
 * 
 * <p>
 * <span style="color:red">?map</span>.?map,??key?value,map(key)?(value),??key
 * </p>
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return mapnullOrEmpty , empty map
 * @see org.apache.commons.collections4.MapUtils#invertMap(Map)
 * @since 1.2.2
 */
public static <K, V> Map<V, K> invertMap(Map<K, V> map) {
    return MapUtils.invertMap(map);
}

From source file:com.feilong.core.util.MapUtil.java

/**
 *  <code>map</code> keyvalue.
 * //  w  w  w . ja v  a 2s  .  c o  m
 * <p>
 * <span style="color:red">?map</span>.<br>
 * ?map,??key?value,map(key)?(value),??key
 * </p>
 * 
 * <h3>:</h3>
 * <blockquote>
 * 
 * <pre class="code">
 * Map{@code <String, Integer>} map = new HashMap{@code <String, Integer>}();
 * map.put("a", 3007);
 * map.put("b", 3001);
 * map.put("c", 3001);
 * map.put("d", 3003);
 * LOGGER.debug(JsonUtil.format(MapUtil.invertMap(map)));
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 * {
 * "3001": "c",
 * "3007": "a",
 * "3003": "d"
 * }
 * </pre>
 * 
 * ? b
 * 
 * </blockquote>
 * 
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return <code>map</code> null, {@link NullPointerException}<br>
 *         <code>map</code> empty,  new HashMap
 * @see org.apache.commons.collections4.MapUtils#invertMap(Map)
 * @since 1.2.2
 */
public static <K, V> Map<V, K> invertMap(Map<K, V> map) {
    return MapUtils.invertMap(map);// HashMap
}

From source file:org.apache.nutch.mapreduce.NutchCounter.java

private void registerCounters(ArrayList<String> names) {
    countersCount.set(names.size());//from  w ww .  ja  v  a  2  s  .  c om

    for (int i = 0; i < countersCount.get(); ++i) {
        counterNames.add(names.get(i));
        counterIndexes.put(names.get(i), i);
        globalCounters.add(new AtomicInteger(0));
        nativeCounters.add(new AtomicInteger(0));
    }

    if (countersCount.get() == 0) {
        LOG.warn("No counters, will not run report thread");
    }

    Validate.isTrue(counterIndexes.size() == counterNames.size());
    Validate.isTrue(counterIndexes.size() == countersCount.get());
    Validate.isTrue(CollectionUtils.containsAll(counterIndexes.keySet(), counterNames));

    LOG.info("Registered counters : " + StringUtils.join(MapUtils.invertMap(counterIndexes).entrySet(), ", "));
}

From source file:org.apache.samza.storage.ContainerStorageManager.java

/**
 * For each standby task, we remove its changeLogSSPs from changelogSSP map and add it to the task's taskSideInputSSPs.
 * The task's sideInputManager will consume and restore these as well.
 *
 * @param containerModel the container's model
 * @param changelogSystemStreams the passed in set of changelogSystemStreams
 * @return A map of changeLogSSP to storeName across all tasks, assuming no two stores have the same changelogSSP
 *///w w w.j av a2 s . c om
private Map<String, SystemStream> getChangelogSystemStreams(ContainerModel containerModel,
        Map<String, SystemStream> changelogSystemStreams) {

    if (MapUtils.invertMap(changelogSystemStreams).size() != changelogSystemStreams.size()) {
        throw new SamzaException("Two stores cannot have the same changelog system-stream");
    }

    Map<SystemStreamPartition, String> changelogSSPToStore = new HashMap<>();
    changelogSystemStreams
            .forEach((storeName, systemStream) -> containerModel.getTasks().forEach((taskName, taskModel) -> {
                changelogSSPToStore.put(
                        new SystemStreamPartition(systemStream, taskModel.getChangelogPartition()), storeName);
            }));

    getTasks(containerModel, TaskMode.Standby).forEach((taskName, taskModel) -> {
        changelogSystemStreams.forEach((storeName, systemStream) -> {
            SystemStreamPartition ssp = new SystemStreamPartition(systemStream,
                    taskModel.getChangelogPartition());
            changelogSSPToStore.remove(ssp);
            this.taskSideInputSSPs.putIfAbsent(taskName, new HashMap<>());
            this.sideInputSystemStreams.put(storeName, Collections.singleton(ssp.getSystemStream()));
            this.taskSideInputSSPs.get(taskName).put(storeName, Collections.singleton(ssp));
        });
    });

    // changelogSystemStreams correspond only to active tasks (since those of standby-tasks moved to side inputs above)
    return MapUtils.invertMap(changelogSSPToStore).entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, x -> x.getValue().getSystemStream()));
}