使用 a Map<String,List<Integer>>,遍历int[]并按索引从String[]数组中获取值并继续填充Map.
的键Map应该是 in 的值,String[]而值是List<Integer>. 检查 中是否存在键Map,如果存在,则将整数值添加到 中,List<Integer>否则以该字符串作为键创建一个条目,并创建一个包含整数值的新列表。
SSCCE将是:
String[] keys = {"apple","orange","apple","orange","nuts"};
int[] values = {1,2,3,4,5};
Map<String, List<Integer>> map = new HashMap<String,List<Integer>>();
for(int i=0;i<values.length;i++) {
int value = values[i];
String key = keys[i];
if(map.containsKey(key)){
map.get(key).add(value);
}
else {
List<Integer> list = new ArrayList<>();
list.add(value);
map.put(key, list);
}
}