6、Map的使用
package Map01;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo01_HashMap {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("cn","中国");
map.put("uk","英国");
map.put("usa","美国");
map.put("usa","美国1");
System.out.println("数据的个数"+map.size());
System.out.println(map.toString());
map.remove("usa");
System.out.println("============使用keyset======================");
for (String key : map.keySet()) {
System.out.println(key+":"+map.get(key));
}
System.out.println("============使用entryset遍历======================");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
public static void main(String[] args) {
HashMap<Students,String> stu=new HashMap<>();
Students p1=new Students("刘德华",20);
Students p2=new Students("李小龙",22);
Students p3=new Students("彭于晏",24);
System.out.println("数据的个数"+stu.size());
System.out.println(stu.toString());
stu.put(p1,"上海");
stu.put(p2,"北京");
stu.put(p3,"深圳");
stu.put(new Students("彭于晏",24),"深圳");
System.out.println("数据的个数"+stu.size());
System.out.println(stu.toString());
stu.remove(p1);
System.out.println("============使用keyset======================");
for (Students key : stu.keySet()) {
System.out.println(key+":"+stu.get(key));
}
System.out.println("============使用entryset遍历======================");
for (Map.Entry<Students, String> entry : stu.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}