我正在尝试构建一个具有排序值的
ImmutableListMultimap
。我尝试了以下方法,但 toImmutableListMultimap
仅支持值映射器,而不支持值映射器列表。还有其他替代方法吗?
Ordering<StatsByDimensions> valueOrdering =
Ordering.natural().onResultOf(StatsByDimensions::getCost);
ImmutableListMultimap<Dimension, StatsByDimensions> statsByDimenisonMap =
Multimaps.index(listOfStats, StatsByDimensions::getDimension)
.asMap()
.entrySet()
.stream()
.collect(
toImmutableListMultimap(
entry -> entry.getKey(),
entry -> valueOrdering.immutableSortedCopy(entry.getValue())));
错误:
incompatible types: inference variable V has incompatible bounds
equality constraints: path.to.StatsByDimensions
lower bounds: com.google.common.collect.ImmutableList<E>javac
当然。 您不需要使用流。
ImmutableListMultimap.Builder<Dimension, StatsByDimensions> statsByDimensionMapBuilder =
ImmutableListMultimap.builder();
statsByDimensionMapBuilder.orderValuesBy(
Comparator.comparing(StatsByDimensions::getCost));
for (StatsByDimensions statsByDimensions : listOfStats) {
statsByDimensionMapBuilder.put(
StatsByDimensions.getDimension(statsByDimensions), statsByDimensions);
}
return statsByDimensionMapBuilder.build();