Having a Multimap sorted on keys only in Java

后端 未结 8 1253
北荒
北荒 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:43

    How about this:

        public static Multimap indexOnScore(Iterable i) {
            Multimap m = Multimaps.index(i, myObjectToScore());
    
            Multimap sortedKeys = Multimaps.newMultimap(
                    Maps.>newTreeMap(),
                    new Supplier>() {
                        @Override
                        public Collection get() {
                            return Lists.newArrayList(); // Or a Set if appropriate
                        }
                    }
            );
    
            sortedKeys.putAll(m);
    
            return sortedKeys;
        }
    

    There would be the overhead of creating two separate Multimaps in this case, though.

提交回复
热议问题