使用 HashMultiset 获取值的 Multimap

发布于 2024-08-19 01:16:12 字数 767 浏览 8 评论 0 原文

我试图拥有一个(基于哈希的)多重映射,其中每个键都有一个(基于哈希的)值的多重集。请参阅示例:

Multimap<Object, Object> mmap = Multimaps.newMultimap(
    Maps.<Object, Collection<Object>>newHashMap(), 
    new Supplier<Collection<Object>>() {
  public Collection<Object> get() {
    return HashMultiset.create();
  }
});
mmap.put("1", "2");

但是,

System.out.println(mmap.get("1") instanceof Multiset<?>); 
//false, the returned collection is not a HashMultiset,
//but a (private) WrappedCollection

看来我无法访问我创建的多重集?我希望能够将其作为 Multiset(包装在 Multisets.unmodifyingMultiset() 中)返回。我也不想每次都将其复制到新的 Multiset 中。除了切换回 Map> 并在代码中添加 Multimap 想要消除的复杂性之外,我还有其他选择吗?

I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for each key. See the example:

Multimap<Object, Object> mmap = Multimaps.newMultimap(
    Maps.<Object, Collection<Object>>newHashMap(), 
    new Supplier<Collection<Object>>() {
  public Collection<Object> get() {
    return HashMultiset.create();
  }
});
mmap.put("1", "2");

But then,

System.out.println(mmap.get("1") instanceof Multiset<?>); 
//false, the returned collection is not a HashMultiset,
//but a (private) WrappedCollection

So it seems I cannot access the multiset I created? I wanted to be able to return that, as a Multiset (wrapped in Multisets.unmodifiableMultiset()). I don't want to copy it into a new Multiset each time either. Do I have any other option than switching back to Map<K, Multiset<V>> and adding in my code the complexity that Multimap meant to eliminate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2024-08-26 01:16:12

恐怕这似乎不可能。您应该提交功能请求。我偷偷怀疑那些狡猾的 Google 人员有一种漂亮的 Multimap,他们可能会发布它,这可能会对您有所帮助。

I'm afraid this doesn't seem to be possible. You should file a feature request. I have a sneaking suspicion those crafty Google folks have a nifty kind of a Multimap that they could potentially release that might potentially help you.

烟柳画桥 2024-08-26 01:16:12

仅限 Multimap 界面指定 get(K) 返回一个 Collection。我认为您的代码依赖或假设其他任何东西都是糟糕的设计。

您能否进一步扩展一下您在这里尝试做的事情?似乎应该有一种更简单的方法来完成它。

The Multimap interface only specifies that get(K) return a Collection<V>. I think it would be poor design for your code to depend or assume anything else.

Can you expand a little bit more on what you are trying to do here? Seems like there should be an easier way to accomplish it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文