Java 自然排序的键的多图,但集合按元素添加的顺序排序
Java Multimap of naturally ordered keys but collections sorted in the order the elements were added
是否有一种简单的方法来实现 Multimap
,它在键采用自然排序的意义上类似于 TreeMultimap
,但在某种意义上又类似于 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();
是否有一种简单的方法来实现 Multimap
,它在键采用自然排序的意义上类似于 TreeMultimap
,但在某种意义上又类似于 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();