Having a Multimap sorted on keys only in Java

后端 未结 8 1234
北荒
北荒 2020-12-03 17:19

I would like to have a c.g.c.c.Multimap that is sorted based on keys only. The values shouldn\'t be sorted. I\'ve tried to build something with guava\'s T

8条回答
  •  失恋的感觉
    2020-12-03 17:40

    Though the OP's specific situation seems to have been answered using immutable multimap building functions, I needed a mutable version of what he was asking for. In case it helps anyone, here's the generic method I ended up creating:

    static  Multimap newTreeArrayListMultimap(
        final int expectedValuesPerKey)
    {
        return Multimaps.newMultimap(new TreeMap>(),
            new Supplier>()
            {
                @Override
                public Collection get()
                {
                    return new ArrayList(expectedValuesPerKey);
                }
            });
    }
    

提交回复
热议问题