java list 转map

Say we have a Map:

Map<String, String> m = new HashMap<String, String>(); m.put("Hello", "World"); m.put("Apple", "3.14"); m.put("Another", "Element");

The keys as a List can be obtained by creating a new ArrayList from a Set returned by the Map.keySet method:

List<String> list = new ArrayList<String>(m.keySet());

While the values as a List can be obtained creating a new ArrayList from a Collection returned by the Map.values method:

List<String> list = new ArrayList<String>(m.values());

The result of getting the List of keys:

Apple
Another
Hello

The result of getting the List of values:

3.14
Element
World

你可能感兴趣的:(java list 转map)