Map集合的特点
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String >();
map.put("xuehao001","xiaoming1");
map.put("xuehao002","xiaoming2");
map.put("xuehao003","xiaoming3");
map.put("xuehao003","xiaoming4");
System.out.println(map);
}
}
Map集合的基本功能
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String >();
map.put("xuehao001","xiaoming1");
map.put("xuehao002","xiaoming2");
map.put("xuehao003","xiaoming3");
System.out.println(map.containsKey("xuehao001"));
System.out.println(map.containsKey("xuehao005"));
System.out.println(map.containsValue("xiaoming1"));
System.out.println(map.isEmpty());
System.out.println(map.size());
System.out.println(map);
}
}
Map集合的获取功能
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String >();
map.put("xuehao001","xiaoming1");
map.put("xuehao002","xiaoming2");
map.put("xuehao003","xiaoming3");
System.out.println(map.get("xuehao001"));
System.out.println(map.get("xuehao"));
Set<String> keySet = map.keySet();
for(String key:keySet){
System.out.println(key);
}
Collection<String> values = map.values();
for (String val:values){
System.out.println(val);
}
System.out.println(map);
}
}
Map集合遍历一
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("xuehao001", "xiaoming1");
map.put("xuehao002", "xiaoming2");
map.put("xuehao003", "xiaoming3");
Set<String> keySet = map.keySet();
for (String key : keySet) {
String val = map.get(key);
System.out.println(key + "," + val);
}
}
}
Map集合遍历二
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("xuehao001", "xiaoming1");
map.put("xuehao002", "xiaoming2");
map.put("xuehao003", "xiaoming3");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String >me : entrySet) {
String key = me.getKey();
String val = me.getValue();
System.out.println(key + "," + val);
}
}
}
ArrayLIst嵌套HashMap
import java.util.*;
public class MapDemo {
public static void main(String[] args) {
ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>();
HashMap<String,String> hm = new HashMap<>();
hm.put("student1","lily1");
hm.put("student2","lily2");
array.add(hm);
HashMap<String,String> hm2 = new HashMap<>();
hm2.put("student21","lily21");
hm2.put("student22","lily22");
array.add(hm2);
HashMap<String,String> hm3 = new HashMap<>();
hm3.put("student31","lily31");
hm3.put("student32","lily32");
array.add(hm3);
for (HashMap<String,String> hsm: array){
Set<String> keySet = hsm.keySet();
System.out.println(keySet);
for (String key:keySet){
String val = hsm.get(key);
System.out.println(key+","+val);
}
}
}
}
HashMap嵌套ArrayList并遍历
import java.util.*;
public class MapDemo {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<>();
ArrayList<String> array1 = new ArrayList<String>();
array1.add("array1a");
array1.add("array1b");
hm.put("三国", array1);
ArrayList<String> array2 = new ArrayList<String>();
array2.add("array2a");
array2.add("array2b");
hm.put("西游", array2);
ArrayList<String> array3 = new ArrayList<String>();
array3.add("array3a");
array3.add("array3b");
hm.put("水浒", array3);
Set<String> keySet = hm.keySet();
for (String Key : keySet) {
System.out.println(Key);
ArrayList<String> val = hm.get(Key);
for (String s : val) {
System.out.println("\t" + s);
}
}
}
}
262
|