|
@@ -2,20 +2,90 @@ package com.sf.day17;
|
|
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
public class Test01 {
|
|
public class Test01 {
|
|
@Test
|
|
@Test
|
|
public void t1(){
|
|
public void t1(){
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
|
- map.put("username","zhangsan");
|
|
|
|
- map.put("username1","zhangsan");
|
|
|
|
- map.put("username2","zhangsan");
|
|
|
|
- map.put("username3","zhangsan");
|
|
|
|
- for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
|
- System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
|
|
|
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
|
+ //map的添加方法
|
|
|
|
+ map.put("username","admin");
|
|
|
|
+ map.put("password","123456");
|
|
|
|
+ System.out.println(map);
|
|
|
|
+
|
|
|
|
+ map.put("username","admin1");
|
|
|
|
+ System.out.println(map);
|
|
|
|
+
|
|
|
|
+ //从map中获取数据
|
|
|
|
+ Object o = map.get("username");
|
|
|
|
+ System.out.println(o);
|
|
|
|
+
|
|
|
|
+ //map中删除数据 返回值是删除的key所对应的value值
|
|
|
|
+ Object username = map.remove("username");
|
|
|
|
+ System.out.println(username);
|
|
|
|
+ System.out.println(map);
|
|
|
|
+
|
|
|
|
+ map.put("age",19);
|
|
|
|
+ map.put("sex","男");
|
|
|
|
+ map.put("id",2323231);
|
|
|
|
+ map.put("weight",190);
|
|
|
|
+
|
|
|
|
+ System.out.println(map);
|
|
|
|
+ //获取当前map集合中的所有key
|
|
|
|
+ Set<String> keys = map.keySet();
|
|
|
|
+ for (String string : keys) {
|
|
|
|
+ System.out.println("key==="+string+"======value"+map.get(string));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ System.out.println("通过Map.entrySet使用iterator遍历key和value:");
|
|
|
|
+ Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
|
|
|
|
+ while (iterator.hasNext()){
|
|
|
|
+// System.out.println(iterator.next());
|
|
|
|
+ Map.Entry<String, Object> next = iterator.next();
|
|
|
|
+ System.out.println(next);
|
|
|
|
+ System.out.println(next.getKey() +"+++++++++++++"+next.getValue());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ System.out.println("通过Map.entrySet遍历key和value");
|
|
|
|
+ for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
|
|
|
|
+ System.out.println(stringObjectEntry);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //只能获取values
|
|
|
|
+ Collection<Object> values = map.values();
|
|
|
|
+ System.out.println("===================================");
|
|
|
|
+ for (Object value : values) {
|
|
|
|
+ System.out.println(value);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void t2(){
|
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
|
+ Map<String,Object> map1 = new HashMap<>();
|
|
|
|
+ map.put("陈泽","国服天使");
|
|
|
|
+ map.put("name","zhangsan");
|
|
|
|
+ map.put("age",19);
|
|
|
|
+ map1.put("sex","男");
|
|
|
|
+ map.putAll(map1);
|
|
|
|
+ System.out.println(map);
|
|
|
|
+ Object val = map.remove("陈泽");
|
|
|
|
+ System.out.println(val);
|
|
|
|
+ System.out.println(map);
|
|
|
|
+// map.clear();
|
|
|
|
+// System.out.println(map);
|
|
|
|
+ System.out.println("====================");
|
|
|
|
+ System.out.println(map.get("name"));
|
|
|
|
+ System.out.println(map.containsKey("name"));
|
|
|
|
+ System.out.println(map.containsValue("zhangsan"));
|
|
|
|
+ System.out.println(map.size());
|
|
|
|
+ System.out.println(map.isEmpty());
|
|
|
|
+ System.out.println(map.equals(map1));
|
|
|
|
+
|
|
|
|
+ Set<String> strings = map.keySet();
|
|
|
|
+ System.out.println(strings);
|
|
|
|
+ System.out.println(map.values());
|
|
|
|
+ System.out.println(map.entrySet());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|