guyanqing 1 سال پیش
والد
کامیت
2aa6af6a20

+ 0 - 1
src/main/java/com/sf/day17/Test01.java

@@ -81,7 +81,6 @@ public class Test01 {
         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());

+ 120 - 0
src/main/java/com/sf/day18/Test01.java

@@ -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"));
+    }
+}

+ 86 - 0
src/main/java/com/sf/day18/User.java

@@ -0,0 +1,86 @@
+package com.sf.day18;
+
+import org.junit.Test;
+
+import java.util.Hashtable;
+import java.util.TreeMap;
+
+/**
+ * 自然排序     如果年龄相等比较姓名unicode
+ */
+public class User implements Comparable{
+    private int age;
+    private String name;
+
+    public User() {
+    }
+
+    public User(int age, String name) {
+        this.age = age;
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "age=" + age +
+                ", name='" + name + '\'' +
+                '}';
+    }
+
+    @Override
+    public int compareTo(Object o) {
+        if(this == o){
+            return 0;
+        }
+        if(o instanceof User){
+            User user = (User) o;
+           int i = this.age - user.age;
+            if (i != 0) {
+            return i;
+            }else {
+                return this.name.compareTo(user.name);
+            }
+            }
+         throw  new RuntimeException("类型不匹配");
+        }
+
+
+    public static void main(String[] args) {
+        /**
+         * treeMap  实现自然排序 是根据key(set集合)进行排序   treeset  实现自然排序
+         */
+        TreeMap<Object,Object> map = new TreeMap<>();
+//        map.put("CC",45);
+//        map.put("MM",78);
+//        map.put("DD",56);
+//        map.put("GG",89);
+//        map.put("JJ",99);
+//        System.out.println(map);
+        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);
+
+    }
+
+
+}

BIN
target/classes/com/sf/day17/Test01.class


BIN
target/classes/com/sf/day18/Test01$1.class


BIN
target/classes/com/sf/day18/Test01.class


BIN
target/classes/com/sf/day18/User.class