从一开始就清楚,我已经“解决”了这个问题(并将在最后发布我的代码)。然而,我的解决方案看起来非常 hacky,包含大量重复代码。
我的问题是:
有没有更好/正确的方法来做到这一点?
我应该使用 HashMap 以外的东西吗?
我应该使用 for 循环以外的东西吗?
我正在尝试做的事情:
我需要创建 16 到 20 个项目(数量各不相同,但我认为这不重要,所以从现在开始我只说 20 个)并且每个项目有 25 个属性(例如“名称”、“power”、level”等)。20 个项目中的每一个都具有相同的属性,但具有不同的(随机)值。
HashMap
似乎是执行此操作的明显方法,但我愿意接受其他建议。我选择使用嵌套的 HashMap
,所以我有包含 20 个键的 outerMap,每个键的值是 1 个项目及其 25 个属性的 HashMap
。因此,更准确地说:我需要创建 20 个具有唯一名称的 HashMaps
,并且每个具有相同的 25 个键但具有不同的(随机)值。
同样,for 循环似乎是执行此操作的明显方法。而且,如果 Java 允许动态创建名称(如 Item+i 表示 Item1),这将是一项简单的任务。但这是不可能的。在尝试循环执行此操作时,我特别遇到了 2 个问题。
命名 20 个
HashMap
。如果我在循环内创建HashMaps
,则无法为它们中的每一个分配唯一的名称(例如 Item1、Item2、Item3 等)。当我尝试使用“put”来存储属性键和值时,我需要当前的
HashMap
名称。与问题 1 基本相同,但代码不同。
这是我想出的解决方案。代码中的注释应该清楚我为什么这样做。想法?
//create outerMap to hold the HashMap for each item
Map<String, Map<String, Object>> itemMap = new LinkedHashMap<>();
//create all possible innerMaps
//manually because you can't name them dynamically in the for loop (example Item+i)
//amount of items unknown, but the range is 16-20
//if less than 20 the remaining HashMaps will simply remain unused
Map<String, Object> Item1 = new LinkedHashMap<>();
Map<String, Object> Item2 = new LinkedHashMap<>();
Map<String, Object> Item3 = new LinkedHashMap<>();
Map<String, Object> Item4 = new LinkedHashMap<>();
Map<String, Object> Item5 = new LinkedHashMap<>();
...through Item20
//for loop that creates and manipulates each of the 25 attributes for each of the 16-20 items
//starting at i = 1 for aesthetic naming purposes
for (i = 1; i < (NumberOfItems + 1); i++)
{
...create and manipulate attribute1
...create and manipulate attribute2
...create and manipulate attribute3
...create and manipulate attribute4
...create and manipulate attribute5
...through attribute25
//you can't call HashMap names dynamically (example Item+i.put(Key, Value)
//so run them through if statements to match i to the correct HashMap
if (i == 1)
{
itemMap.put("Item1", Item1);
Item1.put("attribute1", value);
Item1.put("attribute2", value);
Item1.put("attribute3", value);
...through attribute25
}
else if (i == 2)
{
itemMap.put("Item2", Item2);
Item2.put("attribute1", value);
Item2.put("attribute2", value);
Item2.put("attribute3", value);
...through attribute25
}
else if (i == 3)
{
itemMap.put("Item3", Item3);
Item3.put("attribute1", value);
Item3.put("attribute2", value);
Item3.put("attribute3", value);
...through attribute25
}
...through i == 20
}
最佳答案
您应该使用 1 个 HashMap,其属性定义为类的实例变量(谁的对象是 HashMap 的值。
ValueObject - 保存您的属性的类。
class ValueObject {
private String attr1;
private String attr2;
...
...
private String attr25;
public void setAttr1(String a1){
attr1 = a1;
}
public String getAttr1(){
return attr1;
}
/* Getters and Setters */
...
...
}
创建 map - 添加的元素数量是动态的。
Map<String, ValueObject> map1 = new LinkedHashMap<>();
for (i = 1; i < (NumberOfItems + 1); i++)
{
ValueObject vo = new ValueObject();
vo.setAttr1("a1");
vo.setAttr2("a2");
vo.setAttr3("a3");
...
...
vo.setAttr25("a25");
map1.put("item"+i,vo);
}
关于Java - 创建多个 HashMap 并使用 for 循环填充它们 - 有更好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40230249/