Map String,List String与List Map String,String遍历
程序员文章站
2024-02-17 16:31:40
...
public static void main(String[] args) {
List<Map<String, String>> mapArrayList = new ArrayList<>();
Map<String, String> map = new HashMap<>();
map.put("1","a");
map.put("2","b");
map.put("3","c");
mapArrayList.add(map);
Map<String, String> map1 = new HashMap<>();
map1.put("x","一");
map1.put("y","二");
map1.put("z","三");
mapArrayList.add(map1);
System.out.println("=========1========");
for (Map<String, String> stringMap : mapArrayList) {
for (String s : stringMap.keySet()) {
System.out.println("key:"+s+" value:"+stringMap.get(s));
}
}
System.out.println("=========2========");
for (int i = 0; i < mapArrayList.size(); i++) {
Map<String, String> stringStringMap = mapArrayList.get(i);
Iterator<String> iterator = stringStringMap.keySet().iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println("key:"+next+" value:"+stringStringMap.get(next));
}
}
System.out.println("=========3========");
//循环list
for (Map<String, String> stringStringMap : mapArrayList) {
//循环map
for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
}
}
System.out.println("===遍历值===4======");
for (Map<String, String> stringMap : mapArrayList) {
for (String value : stringMap.values()) {
System.out.println("value值:"+value);
}
}
System.out.println("===只获取键===5======");
for (Map<String, String> stringStringMap : mapArrayList) {
Set<String> strings = stringStringMap.keySet();
System.out.println(strings);
}
}
输出结果
=========1========
key:1 value:a
key:2 value:b
key:3 value:c
key:x value:一
key:y value:二
key:z value:三
=========2========
key:1 value:a
key:2 value:b
key:3 value:c
key:x value:一
key:y value:二
key:z value:三
=========3========
key:1 value:a
key:2 value:b
key:3 value:c
key:x value:一
key:y value:二
key:z value:三
===遍历值===4======
value值:a
value值:b
value值:c
value值:一
value值:二
value值:三
===只获取键===5======
[1, 2, 3]
[x, y, z]
public static void main(String[] args) {
Map<String,List<String>> hashMap = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("第一个");
list.add("第二个");
list.add("第三个");
List<String> list1 = new ArrayList<>();
list1.add("Oklahoma City Thunder");
list1.add("Indiana Pacers");
list1.add("Orlando Magic");
List<String> list2 = new ArrayList<>();
list2.add("Arsenal Football Club");
list2.add("Manchester United Football Club");
list2.add("Fútbol Club Barcelona");
hashMap.put("1", list);
hashMap.put("2", list1);
hashMap.put("3", list2);
System.out.println("=======1=======");
for (String s : hashMap.keySet()) {
System.out.println("key:"+s+" value:"+hashMap.get(s));
}
System.out.println("=======2=======");
Iterator<Map.Entry<String, List<String>>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, List<String>> next = iterator.next();
System.out.println("key:"+next.getKey()+" value:"+next.getValue());
}
System.out.println("=======3=======");
for (Map.Entry<String, List<String>> listEntry : hashMap.entrySet()) {
System.out.println("key:"+listEntry.getKey()+" value:"+listEntry.getValue());
}
System.out.println("=======4=======");
for (List<String> value : hashMap.values()) {
System.out.println(value);
}
}
输出结果
=======1=======
key:1 value:[第一个, 第二个, 第三个]
key:2 value:[Oklahoma City Thunder, Indiana Pacers, Orlando Magic]
key:3 value:[Arsenal Football Club, Manchester United Football Club, Fútbol Club Barcelona]
=======2=======
key:1 value:[第一个, 第二个, 第三个]
key:2 value:[Oklahoma City Thunder, Indiana Pacers, Orlando Magic]
key:3 value:[Arsenal Football Club, Manchester United Football Club, Fútbol Club Barcelona]
=======3=======
key:1 value:[第一个, 第二个, 第三个]
key:2 value:[Oklahoma City Thunder, Indiana Pacers, Orlando Magic]
key:3 value:[Arsenal Football Club, Manchester United Football Club, Fútbol Club Barcelona]
=======4=======
[第一个, 第二个, 第三个]
[Oklahoma City Thunder, Indiana Pacers, Orlando Magic]
[Arsenal Football Club, Manchester United Football Club, Fútbol Club Barcelona]
推荐阅读
-
Map String,List String与List Map String,String遍历
-
List<Map<String, String>>和Map<String, List<String>>遍历
-
jsp遍历List
-
map转string与string转map
-
【redis,1】java操作redis:将string、list、map、自定义的对象
-
数据类型之 String List
-
【redis,1】java操作redis:将string、list、map、自定义的对象
-
C# LIST和STRING互相转换
-
C# LIST和STRING互相转换
-
C# List
如何根据分隔符合并成字符串