|
@@ -0,0 +1,120 @@
|
|
|
+package com.sf.day18;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+import org.w3c.dom.ls.LSInput;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+public class Test01 {
|
|
|
+ /**
|
|
|
+ * linkedHashMap
|
|
|
+ */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t1(){
|
|
|
+ LinkedHashMap<String,Object> map = new LinkedHashMap<>();
|
|
|
+ map.put("王五",13900);
|
|
|
+ map.put("张三",10000);
|
|
|
+ /**
|
|
|
+ * 张三 这个key相同 覆盖原有的value值
|
|
|
+ */
|
|
|
+ map.put("张三",10001);
|
|
|
+ map.put("李四",9000);
|
|
|
+ System.out.println(map);
|
|
|
+ map.put(null,null);
|
|
|
+ System.out.println(map);
|
|
|
+ /**
|
|
|
+ * 遍历的几种方式
|
|
|
+ */
|
|
|
+ for (String key : map.keySet()) {
|
|
|
+ System.out.println(key+"==="+map.get(key));
|
|
|
+ }
|
|
|
+
|
|
|
+ //第二种
|
|
|
+ for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
|
|
|
+ System.out.println(stringObjectEntry);
|
|
|
+ System.out.println(stringObjectEntry.getKey());
|
|
|
+ System.out.println(stringObjectEntry.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ //第三种
|
|
|
+ Collection<Object> values = map.values();
|
|
|
+ for (Object value : values) {
|
|
|
+ System.out.println(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 迭代器
|
|
|
+ */
|
|
|
+ Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ iterator.next();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t2(){
|
|
|
+ List list = new ArrayList();
|
|
|
+ list.add(1);
|
|
|
+ Iterator iterator = list.iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ iterator.next();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定制排序
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t3(){
|
|
|
+ TreeMap map = new TreeMap(new Comparator() {
|
|
|
+ @Override
|
|
|
+ public int compare(Object o1, Object o2) {
|
|
|
+ if(o1 instanceof User && o2 instanceof User){
|
|
|
+ User user1 = (User) o1;
|
|
|
+ User user2 = (User) o2;
|
|
|
+ int value = user1.getAge() - user2.getAge();
|
|
|
+ if(value != 0){
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ return user1.getName().compareTo(user2.getName());
|
|
|
+ }
|
|
|
+ throw new RuntimeException("类型不匹配");
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ map.put(new User(12,"zhangsan"),12);
|
|
|
+ map.put(new User(10,"wang"),12);
|
|
|
+ map.put(new User(15,"li"),12);
|
|
|
+ map.put(new User(19,"qian"),12);
|
|
|
+ map.put(new User(10,"jin"),12);
|
|
|
+ System.out.println(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t4(){
|
|
|
+ // hashtable是线程安全的 但是效率比较低 全局锁 hashmap是线程不安全的
|
|
|
+ // ConcurrentHashMap() 来代替 hashmap 效率比较高 是分段锁
|
|
|
+
|
|
|
+ Map map = new ConcurrentHashMap();
|
|
|
+ Hashtable hashtable = new Hashtable();
|
|
|
+ hashtable.put("username","admin");
|
|
|
+ hashtable.put("age","17");
|
|
|
+ hashtable.put("weight","170");
|
|
|
+// hashtable.put(null,null); hashtable不允许存入null值
|
|
|
+ System.out.println(hashtable);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t5(){
|
|
|
+ Properties properties = System.getProperties();
|
|
|
+// Properties properties1 = new Properties();
|
|
|
+ String property = properties.getProperty("file.encoding");
|
|
|
+ System.out.println(property);
|
|
|
+ properties.setProperty("username","admin");
|
|
|
+ System.out.println(properties.get("username"));
|
|
|
+ }
|
|
|
+}
|