简体   繁体   English

番石榴不同的多图值

[英]guava distinct multimap values

I have a guava HashMultimap like this 我有像这样的番石榴HashMultimap

Multimap<String, String> multiMap = HashMultimap.create();
multiMap.put("a", "x");
multiMap.put("a", "y");
multiMap.put("b", "y");
multiMap.put("b", "z");
multiMap.put("d", "x");
multiMap.put("c", "h");

and I want to create a table with the distinct key and values. 我想创建一个具有不同键和值的表。 Calling 调用

multiMap.values(); // returns x,y,y,z,x,h

but I want to the distinct set of values?. 但我想要一套独特的价值观?

I'm aware that Iterator.filter() is available but am unsure how it should be implmented. 我知道Iterator.filter()是可用的,但不确定它应该如何实现。 The expected result should be 预期的结果应该是

// x,y,z,h

I'd just use 我只是用

Sets.newHashSet(multiMap.values());

or, if you want an immutable version: 或者,如果你想要一个不可变的版本:

ImmutableSet.copyOf(multiMap.values());

Note that in either case you no longer have a live view of the values but a copy. 请注意,在任何一种情况下,您都不再拥有值的实时视图,而是副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM