黑马程序员——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 map = new HashMap();
        map.put(1, "test01");
        map.put(2, "test02");
        map.put(3, "test03");
        map.put(4, "test04");
        Set keySet = map.keySet();
        Iterator 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 map = new HashMap();
        map.put(1, "test01");
        map.put(2, "test02");
        map.put(3, "test03");
        map.put(4, "test04");
        Set> entrySet = map.entrySet();
        Iterator> it = entrySet.iterator();
        while(it.hasNext()){
            Map.Entry 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 map = new HashMap();
        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 s = map.keySet();
        Iterator it = s.iterator();
        while(it.hasNext()){
            System.out.println(map.get(it.next()));
        }
        //entrySet
        Set> se = map.entrySet();
        Iterator> its = se.iterator();
        while(its.hasNext()){
            Map.Entry entry = its.next();
            Student ss = entry.getKey();
            String add = entry.getValue();
            System.out.println(ss + "::" + add);
        }
    }
}
class Student implements Comparable{
    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 tm = new TreeMap(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> s = tm.entrySet();
        Iterator> it = s.iterator();
        while(it.hasNext()){
            Map.Entry me = it.next();
            String name = me.getKey().getName();
            int age = me.getKey().getAge();
            System.out.println(name + "::" + age);
        }
    }
}
class StuNameComp implements Comparator{
    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 tm = new TreeMap();
        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> entrySet = tm.entrySet();
        Iterator> it = entrySet.iterator();
        while(it.hasNext()){
            Map.Entry 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> xx = new HashMap>();
        List yure = new ArrayList();
        List jiuye = new ArrayList();
        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 it = xx.keySet().iterator();
        while(it.hasNext()){
            String roomName = it.next();
            System.out.println(roomName);
            getInfo(xx.get(roomName));
        }
    }
    public static void getInfo (List list){
        Iterator 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;
    }
}

你可能感兴趣的:(java自学笔记)