从哈希映射到两个数组中获取两组值,快速简便

时间:2020-12-12 12:20:08

how in an easy and fast way can I get the two sets of values from a hashmap and load them into two separate arrays? ordering is not important.

如何以简单快捷的方式从哈希映射中获取两组值并将它们加载到两个单独的数组中?订购并不重要。

example from the hashmap

来自hashmap的示例

 HashMap<Integer, String> testHash = new HashMap<Integer, String>();
 testHash.put(666, "skullCrusher");
 testHash.put(808, "octabon");
 testHash.put(69, "bloodyHymen");
 testHash.put(23, "chopSticks");
 testHash.pot(100, "prolapse");

ArrayList<Integer> array1 = new ArrayList<Integer>();

ArrayList<String> array2 = new ArrayList<String>();

the resulting ArrayLists would look like this, and items can be in any order

生成的ArrayLists看起来像这样,并且项目可以按任何顺序排列

 array1 [666, 808, 69, 23, 100]

 array2 [skullCrusher, octabon, bloodyHymen, chopSticks, prolapse]

4 个解决方案

#1


4  

To add the keys of the map to a specific list, use:

要将地图的键添加到特定列表,请使用:

list.addAll(map.keySet());

And for the values:

对于价值观:

list.addAll(map.values());

#2


1  

You can do this :

你可以这样做 :

List<Integer> keys = new ArrayList<Integer>(testHash.keySet());
List<String>  values = new ArrayList<String>(testHash.values());

If the Lists already exists , then :

如果列表已存在,则:

ArrayList<Integer> array1 = new ArrayList<Integer>();
ArrayList<String> array2 = new ArrayList<String>();
array1.addAll(testHash.keySet());
array2.addAll(testHash.values());

#3


1  

If you want particularly arrays:

如果你想要特别的数组:

List<Integer> keys = new ArrayList<Integer>(testHash.keySet());
List<String>  values = new ArrayList<String>(testHash.values());

Then:

然后:

Integer[] ar1 = keys.toArray(new Integer[keys.size()]);
String[] ar2 = values.toArray(new String[values.size()]);

#4


1  

May this help you:

愿这可以帮到你:

ArrayList<Integer> array1 = new ArrayList<Integer>();
ArrayList<String> array2 = new ArrayList<String>();

array1.addAll(testHash.keySet());
array2.addAll(testHash.values());

#1


4  

To add the keys of the map to a specific list, use:

要将地图的键添加到特定列表,请使用:

list.addAll(map.keySet());

And for the values:

对于价值观:

list.addAll(map.values());

#2


1  

You can do this :

你可以这样做 :

List<Integer> keys = new ArrayList<Integer>(testHash.keySet());
List<String>  values = new ArrayList<String>(testHash.values());

If the Lists already exists , then :

如果列表已存在,则:

ArrayList<Integer> array1 = new ArrayList<Integer>();
ArrayList<String> array2 = new ArrayList<String>();
array1.addAll(testHash.keySet());
array2.addAll(testHash.values());

#3


1  

If you want particularly arrays:

如果你想要特别的数组:

List<Integer> keys = new ArrayList<Integer>(testHash.keySet());
List<String>  values = new ArrayList<String>(testHash.values());

Then:

然后:

Integer[] ar1 = keys.toArray(new Integer[keys.size()]);
String[] ar2 = values.toArray(new String[values.size()]);

#4


1  

May this help you:

愿这可以帮到你:

ArrayList<Integer> array1 = new ArrayList<Integer>();
ArrayList<String> array2 = new ArrayList<String>();

array1.addAll(testHash.keySet());
array2.addAll(testHash.values());