代码实现:(jdk:1.8.0)
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
HashMap<Integer,String> mapObj = new HashMap<>();
mapObj.put(1,"first");
mapObj.put(2,"second");
mapObj.put(3,"three");
mapObj.put(4,"four");
// 将HashMap的Values或者keys变成List<T>
List<String> vs = mapObj.values().stream().collect(Collectors.toList());
System.out.println(vs); // [first, second, three, four]
List<Integer> ks = mapObj.keySet().stream().collect(Collectors.toList());
System.out.println(ks); // [1, 2, 3, 4]
}
}