是否有一种简单的方法来实现类似于 TreeMultimap
的 Multimap
,因为键采用自然排序,但也类似于 ArrayListMultimap
是指集合按照添加值的顺序排序吗?
最佳答案
您可以使用MultimapBuilder
创建您需要的功能组合。来自javadoc:
A builder for a multimap implementation that allows customization of the backing map and value collection implementations used in a particular multimap.
This can be used to easily configure multimap data structure implementations not provided explicitly in
com.google.common.collect
, for example:ListMultimap<String, Integer> treeListMultimap = MultimapBuilder.treeKeys().arrayListValues().build(); SetMultimap<Integer, MyEnum> hashEnumMultimap = MultimapBuilder.hashKeys().enumSetValues(MyEnum.class).build();
关于Java Multimap 具有自然排序的键,但集合按添加元素的顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383915/