1 通过获取keyset
通过获取keyset制成迭代器,以迭代器的方式把map值添加到list中,相关代码如下:
List<String> returnResult1 = new ArrayList<String>();
// 获取所有的hashMap键
Set<Integer> keySet = map.keySet();
// 制作关键字迭代器
Iterator<Integer> it = keySet.iterator();
while(it.hasNext()) {
// 把对应键的值添加到list中
returnResult1.add(map.get(it.next()));
}
2 直接取出values
直接取值制成相应的迭代器,再以迭代器的方式把值保存到list中,相关代码如下:
List<String> returnResult2 = new LinkedList<String>();
Collection<String> values = map.values();
Iterator<String> it2 = values.iterator();
while(it2.hasNext()) {
returnResult2.add(it2.next());
}
3 取出entrySet
利用map的entrySet的方法制成相应的迭代器,利用迭代器的方式把相应的值加入到list中去。相关代码如下:
List<String> returnResult3 = new LinkedList<String>();
Set<Entry<Integer, String>> eSet = map.entrySet();
Iterator<Entry<Integer, String>> it3 = eSet.iterator();
while(it3.hasNext()) {
returnResult3.add(it3.next().getValue());
}