Eckd Eckd - 1 year ago 105
Java Question

Editable multimap index

I am having hard time looking for constructor to build mutable

. My code is:

Multimap<String, DbEntity> multimapByKey = Multimaps.index(goodHosts, instanceGetKeyfunction);


// throws
// java.lang.UnsupportedOperationException
// at Source)
// at Source)

Since index returns
I really cannot modify it. However, I cannot see other options to do group by keyFunction for Multimaps on the official documentation.

Answer Source

You can create a method that return a mutable Multimap, like the index function, like this:

public static <K, V> Multimap<K, V> indexMutable(Iterable<V> values,
        Function<? super V, K> function) {

    // check null value, function
    Multimap<K, V> map = ArrayListMultimap.create();

    for (V v : values) {
        // check null V
        map.put(function.apply(v), v);

    return map;

And use like this:

public void testMutableMap() throws Exception {

    List<String> badGuys = Arrays.asList("Inky", "Blinky", "Pinky",
            "Pinky", "Clyde");
    Function<String, Integer> stringLengthFunction = new Function<String, Integer>() {

        public Integer apply(String input) {
            return input.length();

    Multimap<Integer, String> multipmap = indexMutable(badGuys,

    System.out.println("It's mutable!");

    for (String guy : badGuys) {


It's output:

{4=[Inky], 5=[Pinky, Pinky, Clyde], 6=[Blinky]}
It's mutable!
{4=[Inky], 5=[Pinky, Pinky, Clyde], 6=[Blinky]}

This example, is the same for the Javadoc of Multimaps#index.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download