带有类值的java hashmap
我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,只需将映射中的
TDSContent
对象调用的getXXX()
方法与访问器相匹配即可。这假设您在每个构造函数参数的TDSContent
上都有访问器。Certainly, just match the
getXXX()
method you call on theTDSContent
object in the map to the accessor. This assumes you have accessors onTDSContent
for each constructor argument.无论您是否将其作为中间步骤分配给变量,它都是一个
TDSContent
。我不太明白
getCL
的第一次实现背后的目的,看起来它完全违背了首先拥有地图的目的。 (我看不到TDS
是在哪里声明的,只是赋值。)不过,如果有“很多”他们。
创建
getCL
和getCM
辅助方法的想法是一个很好的想法,并且可以大大提高程序的可读性。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 whereTDS
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.
The idea of creating
getCL
andgetCM
helper methods is a good one, and can greatly increase program readability.