java 字符串数组转成HashMap集合对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6907899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Array Into HashMap Collection Object
提问by J?cob
I have the following
我有以下
String[] temp;
which returns
返回
red
blue
green
I would like to add the string array into a collection obejct like HashMap so that I could retrieve values in any class like
我想将字符串数组添加到像 HashMap 这样的集合对象中,以便我可以检索任何类中的值,例如
HashMap hash = New HashMap();
hash.get("red");
hash.get("blue");
hash.get("green");
How can I do this?
我怎样才能做到这一点?
Thanks
谢谢
Update 1
更新 1
String str = "red,blue,green";
String[] temp;
String delimiter = ",";
temp = str.split(delimiter);
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
With the above code, I would like to retrieve values based on values in array. E.g. I would like to get the values in from another class by calling hash.get("One"), which would return red, hash.get("Two") which would return blue and so forth.
使用上面的代码,我想根据数组中的值检索值。例如,我想通过调用 hash.get("One") 从另一个类中获取值,它会返回红色,hash.get("Two") 会返回蓝色等等。
回答by Swagatika
Map<String, String> hash = new HashMap<String, String>();
for(i = 0 ; i < temp.length(); i++)
{
hash.put(temp[i], temp[i]);
}
Then you can retrieve from map hash.get (temp[i]);
然后你可以从 map hash.get (temp[i]); 中检索
回答by ngesh
HashMap<String, String>() map = new HashMap<String, String>();
map.put(temp[i], temp[i]);//here i have considered key as the value itself.. u can use something else //also.
回答by djna
My doubt how I do map temp[i] with red, blue or green?
我怀疑我如何用红色、蓝色或绿色映射 temp[i]?
Using a hash map won't solve this problem directly. Currently you need to write
使用哈希映射不会直接解决这个问题。目前你需要写
temp[ someNumberHere ];
so
所以
temp[ 1 ];
yields a String "blue"
产生一个字符串“蓝色”
If you have a hashMap then instead you might write
如果你有一个 hashMap 那么你可能会写
myColourMap.get( someNumberHere );
so
所以
myColourMap.get( 1 );
Would yield "blue". In either case you are converting a value to a corresponding string, but you do need to know that "someNumber". If you want "blue" you need to know to ask for number 1.
会产生“蓝色”。在任何一种情况下,您都将一个值转换为相应的字符串,但您确实需要知道“someNumber”。如果你想要“蓝色”,你需要知道要 1 号。
It may be that what you need is to use nicely named constant values:
可能您需要的是使用命名良好的常量值:
public Class Colours {
public static final int RED = 0;
public static final int BLUE = 1;
public static final int GREEN = 1;
// plus either the array of strings or the hashMap
public statuc String getColour(int colourNumber ) {
return myArray[colourNumber]; // or myMap.get(colourNumber)
}
}
Your clients can now write code such as
您的客户现在可以编写代码,例如
Colours.getColour( Colour.RED );
[It is better to use enums than just raw ints, but let's not divert from arrays and hashMaps right now].
[使用枚举比仅使用原始整数更好,但我们现在不要从数组和 hashMaps 转向]。
Now when might you prefer a hashMap instead of an array? Consider that you might have more colours, for example 12695295 might be "light pink" and 16443110 might be "lavender".
现在你什么时候更喜欢 hashMap 而不是数组?考虑您可能有更多颜色,例如 12695295 可能是“浅粉色”,而 16443110 可能是“薰衣草”。
Now you really don't want an array with 16,443,110 entries when you are only using perhaps 500 of them. Now a HashMap is a really useful thing
现在,当您只使用大约 500 个条目时,您真的不想要一个包含 16,443,110 个条目的数组。现在 HashMap 是一个非常有用的东西
myMap.put( Colour.LAVENDER, 16443110 );
and so on.
等等。