我们首先创建一个具有键值对的LinkedHashMap-
Map<String, String>map = new LinkedHashMap<String, String>(); map.put("1", "Katie"); map.put("2", "Peter"); map.put("3", "Amy"); map.put("4", "Kane"); map.put("5", "Colin"); map.put("6", "Andre"); map.put("7", "Pollard"); map.put("8", "David"); map.put("9", "Jofra"); map.put("10", "Akila");
现在,创建一个新的List并将键存储在上面的Map中-
List<String>list = new ArrayList<String>(); list.addAll(map.keySet());
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Demo { public static void main(String[] args) { Map<String, String>map = new LinkedHashMap<String, String>(); map.put("1", "Katie"); map.put("2", "Peter"); map.put("3", "Amy"); map.put("4", "Kane"); map.put("5", "Colin"); map.put("6", "Andre"); map.put("7", "Pollard"); map.put("8", "David"); map.put("9", "Jofra"); map.put("10", "Akila"); List<String>list = new ArrayList<String>(); list.addAll(map.keySet()); System.out.println("Keys..."); for (String str: list) { System.out.println(str); } } }
输出结果
Keys... 1 2 3 4 5 6 7 8 9 10