【195天】黑马程序员27天视频学习笔记【Day18-中】

叨逼叨两句

~

18-06:LinkedHashMap的概述和使用

  1. 怎么存怎么取
package com.test.demo001;

import java.util.LinkedHashMap;

public class Demo002 {
    public static void main(String[] args) {
        LinkedHashMap<String,Integer> lhm = new LinkedHashMap<>();
        lhm.put("张3", 23);
        lhm.put("张4", 24);
        lhm.put("张5", 25);
        lhm.put("张6", 26);
        System.out.println(lhm);
    }
}

18-07:TreeMap集合键是Student值是String的案例

  1. 用Comparator(比较器)
package com.test.demo001;

import java.util.Comparator;
import java.util.TreeMap;

public class Demo002 {
    public static void main(String[] args) {
        TreeMap<Student,String> tm = new TreeMap<>(new Comparator<Student>(){

            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge() - o2.getAge();
                return num == 0 ? o1.getName().compareTo(o2.getName()) : num;
            }
        });
        
        tm.put(new Student("z3",23), "B");
        tm.put(new Student("z4",24), "C");
        tm.put(new Student("z5",25), "D");
        tm.put(new Student("z6",26), "E");
        
        System.out.println(tm);
        
    }
    
    
    
    
}
  1. 用Comparable
package com.test.demo001;

import java.util.Comparator;
import java.util.TreeMap;

public class Demo002 {
    public static void main(String[] args) {
        TreeMap<Student,String> tm = new TreeMap<>();
        
        tm.put(new Student("z3",23), "B");
        tm.put(new Student("z4",24), "C");
        tm.put(new Student("z5",25), "D");
        tm.put(new Student("z6",26), "E");
        
        System.out.println(tm);
        
    }
}
package com.test.demo001;

public class Student implements Comparable<Student>{
    private String name;
    private int age;
    
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    
    public int compareTo(Student o) {
        int num = this.age - o.age;
        return num == 0? this.name.compareTo(o.name) : num;
    }
    
}

18-08:统计字符串中每个字符出现的次数

package com.test.demo001;

import java.util.HashMap;

public class Demo002 {
    public static void main(String[] args) {
        String s = "aaaaabbbbccccc";
        char[] arr = s.toCharArray();
        HashMap<Character,Integer> hm = new HashMap<>();
        for(char c: arr){
            /*if(!hm.containsKey(c)){
                hm.put(c, 1);
            } else {
                hm.put(c, hm.get(c)+1);
            }*/
            hm.put(c,!hm.containsKey(c)? 1 :  hm.get(c)+1 );
        }
        for(Character key : hm.keySet()){
            System.out.println(key + "=" + hm.get(key));
        }
        
    }
    
    
    
    
}

18-09:HashMap中嵌套HashMap

package com.test.demo001;

import java.util.HashMap;

public class Demo002 {
    /**
     * @param args
     * 学校——班级——学生
     */
    public static void main(String[] args) {
        HashMap<Student,String> hm273 = new HashMap<>();        //往班级存入学生
        hm273.put(new Student("z4",23), "BeiJing");
        hm273.put(new Student("z5",23), "ShangHai");
        hm273.put(new Student("z3",23), "BeiJing");    
        hm273.put(new Student("z6",23), "ShenZhen");
        
        HashMap<Student,String> hm284 = new HashMap<>();        //往班级存入学生
        hm284.put(new Student("k5",23), "ShangHai1");
        hm284.put(new Student("k6",23), "ShenZhen1");
        hm284.put(new Student("k3",23), "BeiJing1");
        hm284.put(new Student("k4",23), "BeiJing1");
        
            
        HashMap<HashMap<Student,String>,String> hm = new HashMap<>(); //往学校存入班级
        hm.put(hm273, "273");
        hm.put(hm284, "284");
        
        for(HashMap<Student,String> key : hm.keySet() ){
            String value1 = hm.get(key);
            for(Student key2: key.keySet()){
                String value2 = key.get(key2);
                System.out.println(key2 + "..." + value1+"..."+value2);
            }
        }
        
        
    }
}

18-10:HashMap与Hashtable的区别

  1. 共同点

    1. 底层都是哈希算法
    2. 都是双列集合
  2. 不同点

    1. HashMap是线程不安全的,效率高,JDK1.2版本;Hashtable是线程安全的,效率低,JDK1.0版本
    2. HashMap可以存储null键和null值;Hashtable不可以存储null键和null值。

注意:Hashtable第二个t不大写。

评论

暂无评论