简体   繁体   English

java中有两个键的数据结构

[英]Data structure having two keys in java

I have a following scenario.我有以下场景。

1) Module A sends parameter to Modele B, and then Module B makes a request to Module C to provide values for that parameter. 1) 模块 A 向模型 B 发送参数,然后模块 B 向模块 C 发出请求以提供该参数的值。 (There can be many requesters ie Module A1,A2 etc. as Module A). (可以有许多请求者,即模块 A1、A2 等作为模块 A)。 The Module C then sends the parameter value to Module B, and then Module B sends the values back to the Module A. Thus Module B is a middleware.然后模块 C 将参数值发送到模块 B,然后模块 B 将值发送回模块 A。因此,模块 B 是一个中间件。

2) Here Module B gives a unique intKey to each requesters (to Module A1,A2 etc.) And Module C gives unique StringKey per requester to the Module B. 2)在此模块B给出了一个独特intKey各请求者(到模块A1,A2等)和模块C给出了每个请求者独特StringKey到模块B.

3) Module B is responsible to give correct values to correct requester. 3) 模块 B 负责为正确的请求者提供正确的值。 That means it has to map StringKey to intKey.这意味着它必须将 StringKey 映射到 intKey。 Currently it does so by creating two concurrent hashmaps.目前它通过创建两个并发哈希图来实现。 i) intKey,StringKey and ii) StringKey,intKey i) intKey,StringKey和 ii) StringKey,intKey

The problem now is that the Module B has to all the time iterate over the Maps to find the correct requester (Module A).现在的问题是模块 B 必须一直遍历 Map 以找到正确的请求者(模块 A)。

I was wondering if I can have a data structure where I have two keys and if I give any one say Key1 then I can retrieve its corresponding key2 and also the other way.我想知道我是否可以拥有一个数据结构,其中我有两个键,如果我给任何人说 Key1,那么我可以检索其相应的 key2 以及其他方式。 Is there any way I can get rid of these two concurrent Hashmaps ?有什么办法可以摆脱这两个并发的 Hashmap 吗? I would highly appreciate any help regarding this.我将非常感谢任何有关这方面的帮助。

Essentially no - you cannot get away from using two maps, concurrent or otherwise, because you are working with two different types of key (unless you decide to use Map<Object,Object> which would be criminal).基本上不 - 您不能避免使用两个地图,并发或其他方式,因为您正在使用两种不同类型的密钥(除非您决定使用Map<Object,Object>这将是犯罪行为)。

But - you can hide the complexity:但是 - 您可以隐藏复杂性:

public interface BiMap<P, Q> {

    public Q putQ(P key, Q value);

    public Q getQ(P key);

    public P putP(P key, Q value);

    public P getP(Q key);
}

Now wrap your two maps in one of those and things look tidier, they just aren't tidy inside.现在将您的两张地图包裹在其中一张中,事情看起来更整洁,只是里面不整洁。

This should work:这应该有效:

public class BiMap<P, Q> {

    final Map<P, Q> pq;
    final Map<Q, P> qp;

    public BiMap(MapFactory maker) {
        pq = maker.<P, Q>make();
        qp = maker.<Q, P>make();
    }

    public BiMap() {
        // Default to ConcurrentHashMap
        this(MapFactory.ConcurrentHashMap);
    }

    public Q putQ(P key, Q value) {
        return pq.put(key, value);
    }

    public Q getQ(P key) {
        return pq.get(key);
    }

    public P putP(Q key, P value) {
        return qp.put(key, value);
    }

    public P getP(Q key) {
        return qp.get(key);
    }

    // Puts both at once.
    public synchronized void put(P p, Q q) {
        putQ(p, q);
        putP(q, p);
    }

    public enum MapFactory {

        ConcurrentHashMap {

                    @Override
                    <P, Q> Map<P, Q> make() {
                        return new java.util.concurrent.ConcurrentHashMap<P,Q>();
                    }

                };

        abstract <P, Q> Map<P, Q> make();
    }
}

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

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