我们有 MultiBiMap 吗?

Do we have a MultiBiMap ?(我们有 MultiBiMap 吗?)
本文介绍了我们有 MultiBiMap 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们现在有 BiMap 和 multiMap 的概念,但是有 multiBiMap 吗?所以我的意思是什么.在 multiMap 中,K 和 V 之间存在一对多关系,单个键可以关联到多个值,因此得名.在双地图中,您有 K,V 对,这是双向的,这意味着您也可以获得 V,K 关系.就像有两个常规地图但同步.我需要一个双向多图,您可以在其中结合这两个概念.

As we now, there is the concept of BiMap and multiMap but is there a multiBiMap ? so what do I mean by this. In multiMap you have one-to-many relationship between K and V, a single key can be associated to multiple value, hence the name. In bi map you have K,V pair which is bi-directional mean you can get V,K relationship as well. Like having a two regular maps but synchronized. I need a bi directional multi map where you combine these two concepts.

推荐答案

import java.util.Set;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

public class ManyToMany<K, V> {
    private final SetMultimap<K, V> keysToValues = HashMultimap.create();

    private final SetMultimap<V, K> valuesToKeys = HashMultimap.create();

    public Set<V> getValues(K key) {
        return keysToValues.get(key);
    }

    public Set<K> getKeys(V value) {
        return valuesToKeys.get(value);
    }

    public boolean put(K key, V value) {
        return keysToValues.put(key, value) && valuesToKeys.put(value, key);
    }

    public boolean putAll(K key, Iterable<? extends V> values) {
        boolean changed = false;
        for (V value : values) {
            changed |= put(key, value);
        }
        return changed;
    }
}

这篇关于我们有 MultiBiMap 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)