带有类值的java hashmap

发布于 2024-12-12 02:54:12 字数 2803 浏览 0 评论 0原文

我需要将 TDSContent 存储到指定 String 键的哈希图中。我需要做这样的事情。

public class Target {
    public Target() {
        final int a = 0x64c896;
        final int b = 0xc050be;
        final int c = 0xffc896;

        TDS = new HashMap<String, TDSContent>();

        TDSContent contentMA = new TDSContent(a, b, c, 10);
        TDSContent contentMB = new TDSContent(a, c, b, 10);
        TDSContent contentMC = new TDSContent(b, a, c, 10);
        TDSContent contentMD = new TDSContent(c, b, a, 10);
        TDSContent contentME = new TDSContent(c, a, b, 10);
        TDSContent contentMF = new TDSContent(b, c, a, 10);
        ... // and so on...

        TDS.put("Marker A", contentMA);
        TDS.put("Marker B", contentMB);
        TDS.put("Marker C", contentMC);
        TDS.put("Marker D", contentMD);
        TDS.put("Marker E", contentME);
        TDS.put("Marker F", contentMF);
            ... // and so on...
    }

    public int getCL(String key) {
        TDSContent tdc = TDS.get(key);

        if(tdc.equals(contentMA)) {
            return contentMA.getValue();
        } else if(tdc.equals(contentMB)) {
            return contentMB.getValue();
        } else if(tdc.equals(contentMC)) {
            return contentMC.getValue();
        } else if(tdc.equals(contentMD)) {
            return contentMD.getValue();
        } else if(tdc.equals(contentME)) {
            return contentME.getValue();
        } else if(tdc.equals(contentMF)) {
            return contentMF.getValue();
        } ...// and so on...

             else {
            return contentMD.getValue();
        }
    }
}

问题是,手动创建 TDSContent 类的对象需要花费大量精力。

我可以做这样的事情...:

public class Target {
    public Target() {
        final int a = 0x64c896;
        final int b = 0xc050be;
        final int c = 0xffc896;

        TDS = new HashMap<String, TDSContent>();

           // form: new TDSContent(CL, CM, CR, D);
        TDS.put("Marker A", new TDSContent(a, b, c, 10));
        TDS.put("Marker B", new TDSContent(a, c, b, 10));
                ... // and so on..
    }

    public int getCL(String key) {
           // this method gets the first parameter of the TDSContent
           // constructor (see comment above).
        return TDS.get(key).getValue();
    }

    public int getCM(String key) {
    ... // similar to getCL but returns the second parameter of the TDSContent
        // constructor (see comment above)

...在 getCL() 上并获取 TDSContent 的实例化[?] 值吗?

具体来说,如果我接到这样的电话:

Target target = new Target();
int x1 = target.getCL("Marker A"); // I should get 0x64c896 here
int x2 = target.getCM("Marker A"); // I should get 0xc050be here

这可能吗?

I need to store a TDSContent into a hashmap specifying a String key. I need to do something like this.

public class Target {
    public Target() {
        final int a = 0x64c896;
        final int b = 0xc050be;
        final int c = 0xffc896;

        TDS = new HashMap<String, TDSContent>();

        TDSContent contentMA = new TDSContent(a, b, c, 10);
        TDSContent contentMB = new TDSContent(a, c, b, 10);
        TDSContent contentMC = new TDSContent(b, a, c, 10);
        TDSContent contentMD = new TDSContent(c, b, a, 10);
        TDSContent contentME = new TDSContent(c, a, b, 10);
        TDSContent contentMF = new TDSContent(b, c, a, 10);
        ... // and so on...

        TDS.put("Marker A", contentMA);
        TDS.put("Marker B", contentMB);
        TDS.put("Marker C", contentMC);
        TDS.put("Marker D", contentMD);
        TDS.put("Marker E", contentME);
        TDS.put("Marker F", contentMF);
            ... // and so on...
    }

    public int getCL(String key) {
        TDSContent tdc = TDS.get(key);

        if(tdc.equals(contentMA)) {
            return contentMA.getValue();
        } else if(tdc.equals(contentMB)) {
            return contentMB.getValue();
        } else if(tdc.equals(contentMC)) {
            return contentMC.getValue();
        } else if(tdc.equals(contentMD)) {
            return contentMD.getValue();
        } else if(tdc.equals(contentME)) {
            return contentME.getValue();
        } else if(tdc.equals(contentMF)) {
            return contentMF.getValue();
        } ...// and so on...

             else {
            return contentMD.getValue();
        }
    }
}

The problem is, it will take so much hard work to manually create an object of class TDSContent.

Can I do something like this ...:

public class Target {
    public Target() {
        final int a = 0x64c896;
        final int b = 0xc050be;
        final int c = 0xffc896;

        TDS = new HashMap<String, TDSContent>();

           // form: new TDSContent(CL, CM, CR, D);
        TDS.put("Marker A", new TDSContent(a, b, c, 10));
        TDS.put("Marker B", new TDSContent(a, c, b, 10));
                ... // and so on..
    }

    public int getCL(String key) {
           // this method gets the first parameter of the TDSContent
           // constructor (see comment above).
        return TDS.get(key).getValue();
    }

    public int getCM(String key) {
    ... // similar to getCL but returns the second parameter of the TDSContent
        // constructor (see comment above)

... on getCL() and get and the instantiated[?] value of TDSContent?

Specifically, if I have a call on something like:

Target target = new Target();
int x1 = target.getCL("Marker A"); // I should get 0x64c896 here
int x2 = target.getCM("Marker A"); // I should get 0xc050be here

Is that possible?

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

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

发布评论

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

评论(2

晨敛清荷 2024-12-19 02:54:12

当然,只需将映射中的 TDSContent 对象调用的 getXXX() 方法与访问器相匹配即可。这假设您在每个构造函数参数的 TDSContent 上都有访问器。

public int getCL(String key) {
    return TDS.get(key).getCL();
}

public int getCM(String key) {
    return TDS.get(key).getCM();
}

public int getCR(String key) {
    return TDS.get(key).getCR();
}

public int getD(String key) {
    return TDS.get(key).getD();
}

Certainly, just match the getXXX() method you call on the TDSContent object in the map to the accessor. This assumes you have accessors on TDSContent for each constructor argument.

public int getCL(String key) {
    return TDS.get(key).getCL();
}

public int getCM(String key) {
    return TDS.get(key).getCM();
}

public int getCR(String key) {
    return TDS.get(key).getCR();
}

public int getD(String key) {
    return TDS.get(key).getD();
}
萧瑟寒风 2024-12-19 02:54:12

无论您是否将其作为中间步骤分配给变量,它都是一个 TDSContent

我不太明白 getCL 的第一次实现背后的目的,看起来它完全违背了首先拥有地图的目的。 (我看不到 TDS 是在哪里声明的,只是赋值。)

不过,如果有“很多”他们。

putTds("Marker B", tds(a, c, b, 10));

创建 getCLgetCM 辅助方法的想法是一个很好的想法,并且可以大大提高程序的可读性。

It's a TDSContent whether or not you assign it to a variable as an intermediate step.

I don't really understand the purpose behind the first implementation of getCL, it seems like it completely defeats the purpose of having a map in the first place. (I don't see where TDS is declared, only assigned.)

I might go a tad bit further and create some handy utility methods to make things even shorter, though, if there's a "lot" of them.

putTds("Marker B", tds(a, c, b, 10));

The idea of creating getCL and getCM helper methods is a good one, and can greatly increase program readability.

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