我们首先创建一个具有键值对的LinkedHashMap-
Map<String, String>map1 = new LinkedHashMap<String, String>(); map1.put("1", "Jim"); map1.put("2", "David"); map1.put("3", "Tom"); map1.put("4", "Sam"); map1.put("5", "Steve");
现在让我们创建另一个具有键值对的LinkedHashMap-
Map<String, String>map2 = new LinkedHashMap<String, String>(); map2.put("6", "Katie"); map2.put("7", "John"); map2.put("8", "Kane"); map2.put("9", "Chris");
现在,创建一个新的List并将键存储在上面的Map中-
List<String>list = new ArrayList<String>(); list.addAll(map1.keySet()); list.addAll(map2.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>map1 = new LinkedHashMap<String, String>(); map1.put("1", "Jim"); map1.put("2", "David"); map1.put("3", "Tom"); map1.put("4", "Sam"); map1.put("5", "Steve"); Map<String, String>map2 = new LinkedHashMap<String, String>(); map2.put("6", "Katie"); map2.put("7", "John"); map2.put("8", "Kane"); map2.put("9", "Chris"); List<String>list = new ArrayList<String>(); list.addAll(map1.keySet()); list.addAll(map2.keySet()); System.out.println("Keys..."); for (String str: list) { System.out.println(str); } } }
输出结果
Keys... 1 2 3 4 5 6 7 8 9