Is there a way to put a 2d array into a Hash map setup?
有没有办法将二维数组放入哈希映射设置?
Example of the array would be two strings {"John", "red"}, {"George", "blue}
数组的示例是两个字符串{“John”,“red”},{“George”,“blue}
And I would want red to correspond to john etc.
我希望红色对应约翰等。
I know I can use a nested loop to go through every item in the 2d array but how would then set it up to add them to the hash-map
我知道我可以使用嵌套循环遍历2d数组中的每个项目但是如何设置它以将它们添加到哈希映射
hashMap.put("John", "red");
2 个解决方案
#1
1
Assuming every array has 2 items in the form of {Name, Color}, you can just iterate over it
假设每个数组都有{Name,Color}形式的2个项目,你可以迭代它
for(String[] combo : some2DArray){
someHashMap.Put(combo[0], combo[1]); // Or swap them, depending on what you
// want to be the key and the value
}
If you want to avoid the possibility of removing data because you happen to have two people with the same name there are a few approaches you can take:
如果您想避免删除数据的可能性,因为您碰巧有两个同名的人,您可以采取以下几种方法:
-
Keep the old data
保留旧数据
-
Keep the new data
保留新数据
-
Assign the new data to a new key
将新数据分配给新密钥
-
Combine the data in the same key
将数据组合在同一个键中
Keep the old data
保留旧数据
Perform a check before using HashMap#put
and see if the key already exists.
Only add the data if it doesn't exist yet.
在使用HashMap #put之前执行检查并查看密钥是否已存在。仅添加数据(如果尚不存在)。
Keep the new data
保留新数据
Use the current code, it will overwrite the old value.
使用当前代码,它将覆盖旧值。
Assign the new data to a new key
将新数据分配给新密钥
Create a new key based on your own rules and insert that.
根据您自己的规则创建一个新密钥并插入。
Combine the data in the same key
将数据组合在同一个键中
Define your HashMap
as HashMap<String, List<String>>
and add the values to the list.
将HashMap定义为HashMap
#2
-2
How about implementing a Pair
class, so you can use HashMap<Pair<String,String>>
?
如何实现Pair类,这样你就可以使用HashMap
EDIT: Might be that I misunderstood your question, is that what yoe were asking?
编辑:可能是我误解了你的问题,是什么哟问的?
#1
1
Assuming every array has 2 items in the form of {Name, Color}, you can just iterate over it
假设每个数组都有{Name,Color}形式的2个项目,你可以迭代它
for(String[] combo : some2DArray){
someHashMap.Put(combo[0], combo[1]); // Or swap them, depending on what you
// want to be the key and the value
}
If you want to avoid the possibility of removing data because you happen to have two people with the same name there are a few approaches you can take:
如果您想避免删除数据的可能性,因为您碰巧有两个同名的人,您可以采取以下几种方法:
-
Keep the old data
保留旧数据
-
Keep the new data
保留新数据
-
Assign the new data to a new key
将新数据分配给新密钥
-
Combine the data in the same key
将数据组合在同一个键中
Keep the old data
保留旧数据
Perform a check before using HashMap#put
and see if the key already exists.
Only add the data if it doesn't exist yet.
在使用HashMap #put之前执行检查并查看密钥是否已存在。仅添加数据(如果尚不存在)。
Keep the new data
保留新数据
Use the current code, it will overwrite the old value.
使用当前代码,它将覆盖旧值。
Assign the new data to a new key
将新数据分配给新密钥
Create a new key based on your own rules and insert that.
根据您自己的规则创建一个新密钥并插入。
Combine the data in the same key
将数据组合在同一个键中
Define your HashMap
as HashMap<String, List<String>>
and add the values to the list.
将HashMap定义为HashMap
#2
-2
How about implementing a Pair
class, so you can use HashMap<Pair<String,String>>
?
如何实现Pair类,这样你就可以使用HashMap
EDIT: Might be that I misunderstood your question, is that what yoe were asking?
编辑:可能是我误解了你的问题,是什么哟问的?