黑馬程序員——Java基礎——集合類_2

集合類

集合_Map概述

該集合存儲鍵值對,且要保證鍵的唯一性
1. 添加:
put(K key, V value)
putAll(Map

集合_Map子類對象概述

Map:
HashTable:
底層是哈希表數據結構,不可以存入null鍵null值
該集合線程同步
JDK1.0、效率低
- - HashMap:
- - - - - - 底層是哈希表數據結構,並允許使用null鍵null值,null可以作爲鍵存在該集合線程不同步
- - - - - - JDK1.2、效率高
- - TreeMap:
- - - - - - 底層是二叉樹數據結構,可以用於給map集合中的鍵進行排序
- - - - - - 該集合線程不同步

集合_Map共性方法、keySet()、entrySet()

Map集合的兩種取出方式:
1. Set keySet(): 將map中所有的鍵存入到Set集合,通過Set類的迭代器取出所有的鍵,再根據get()方法,獲取鍵對應的值

public class TestKetSet {
    public static void main(String[] args){
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "test01");
        map.put(2, "test02");
        map.put(3, "test03");
        map.put(4, "test04");
        Set<Integer> keySet = map.keySet();
        Iterator<Integer> it = keySet.iterator();
        while(it.hasNext()){
            Integer key = it.next();
            System.out.println(map.get(key));
        }
    }
}
  1. Set
public class TestKetSet {
    public static void main(String[] args){
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "test01");
        map.put(2, "test02");
        map.put(3, "test03");
        map.put(4, "test04");
        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();
        while(it.hasNext()){
            Map.Entry<Integer, String> entry = it.next();
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "::" + value);
        }   
    }
}

集合_Map練習

需求:
每一個學生都有歸屬地,學生Student、地址String
學生有屬性姓名和年齡(姓名和年齡相同視爲同一個學生)

public class TestMap {
    public static void main(String[] args){
        Map<Student, String> map = new HashMap<Student, String>();
        map.put(new Student("test01", 18), "beijing");
        map.put(new Student("test02", 19), "shanghai");
        map.put(new Student("test03", 20), "beijing");
        map.put(new Student("test04", 21), "shanghai");
        //map.put(new Student("test01", 18), "beijing");
        //ketSet
        Set<Student> s = map.keySet();
        Iterator<Student> it = s.iterator();
        while(it.hasNext()){
            System.out.println(map.get(it.next()));
        }
        //entrySet
        Set<Map.Entry<Student, String>> se = map.entrySet();
        Iterator<Map.Entry<Student, String>> its = se.iterator();
        while(its.hasNext()){
            Map.Entry<Student, String> entry = its.next();
            Student ss = entry.getKey();
            String add = entry.getValue();
            System.out.println(ss + "::" + add);
        }
    }
}
class Student implements Comparable<Student>{
    private String name;
    private int age;
    Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public String toString(){
        return name + "::" + age;
    }
    public int hashCode(){
        return name.hashCode() + age*34;
    }
    public boolean equals(Object obj){
        if(!(obj instanceof Student))
            throw new ClassCastException("不同類不要比");
        Student s = (Student)obj;
        return this.name.equals(s.name) && this.age == s.age;
    }
    public int compareTo(Student s) {
        int num = new Integer(this.age).compareTo(s.age);
        if(num == 0){
            return this.name.compareTo(s.name);
        }
        return num;
    }
}

集合_TreeMap練習

對學生對象的年齡進行升序排序

public class TestTreeMap {
    public static void main(String[] args){
        Map<Student, String> tm = new TreeMap<Student, String>(new StuNameComp());
        tm.put(new Student("test03", 18), "beijing");
        tm.put(new Student("test02", 20), "shanghai");
        tm.put(new Student("test04", 39), "beijing");
        tm.put(new Student("test01", 3), "shanghai");
        Set<Map.Entry<Student, String>> s = tm.entrySet();
        Iterator<Map.Entry<Student, String>> it = s.iterator();
        while(it.hasNext()){
            Map.Entry<Student, String> me = it.next();
            String name = me.getKey().getName();
            int age = me.getKey().getAge();
            System.out.println(name + "::" + age);
        }
    }
}
class StuNameComp implements Comparator<Student>{
    public int compare(Student s1, Student s2) {
        int num = s1.getName().compareTo(s2.getName());
        if(num == 0)
            return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
        return num;
    }
}

集合_TreeMap練習2

獲取字符串中,字母出現的次數
打印的結果爲a(x), b(y), c(z)
當需要有映射關係時,可以選擇map集合

public class TestMap2 {
    public static void main(String[] args){
        String s = charCount("adsfdyjfjdrhdgfyjshrhsdfaesd");
        System.out.println(s);
    }
    public static String charCount(String str){
        char[] chs = str.toCharArray();
        TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
        int count = 0;
        for(int i = 0; i < chs.length; i++){
            Integer value = tm.get(chs[i]);
            if(value != null)
                count = value;
            count++;
            tm.put(chs[i], count);
            count = 0;
        }
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
        Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();
        while(it.hasNext()){
            Map.Entry<Character, Integer> entry = it.next();
            Character ch = entry.getKey();
            Integer value = entry.getValue();
            sb.append(ch + "(" + value + ")");
        }
        return sb.toString();
    }
}

集合_Map擴展

public class MapInMap2 {
    public static void main(String[] args){
        HashMap<String, List<Students>> xx = new HashMap<String, List<Students>>();
        List<Students> yure = new ArrayList<Students>();
        List<Students> jiuye = new ArrayList<Students>();
        xx.put("yure", yure);
        xx.put("jiuye", jiuye);
        yure.add(new Students("001", "zhangsan"));
        yure.add(new Students("002", "lisi"));
        jiuye.add(new Students("001", "wangwu"));
        jiuye.add(new Students("002", "zhaoliu"));

        Iterator<String> it = xx.keySet().iterator();
        while(it.hasNext()){
            String roomName = it.next();
            System.out.println(roomName);
            getInfo(xx.get(roomName));
        }
    }
    public static void getInfo (List<Students> list){
        Iterator<Students> it = list.iterator();
        while(it.hasNext()){
            Students s = it.next();
            System.out.println(s.getID() + "::" + s.getName());
        }
    }
}
class Students{
    private String id;
    private String name;
    Students(String id, String name){
        this.id = id;
        this.name = name;
    }
    public String getID(){
        return id;
    }
    public String getName(){
        return name;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章