黑馬程序員-Map集合部分

——- android培訓java培訓、期待與您交流! ———-

Map集合,與Collection集合的不同?

Map集合主要用來存放鍵-值對的,把鍵-值這一對映射存放進去。

(一)Map集合的常用方法:

1)put(key,alue)
putAll()添加
添加元素,當添加兩個相同的鍵,後來的將覆蓋原有值,並put(key,value)返回被覆蓋的值。
2)clear清空集合
remove(key)刪除集合中某一個元素
3)判斷:
containsKey()是否存在某個鍵
containsValue()是否存在某個值
isEmpty()集合是否爲空
4)獲取
get(key)通過某個鍵獲取對應的值
size()獲取集合的大小
alues()獲取集合中所有的值
5)集合中輸出元素的兩種方法。Map集合中取出原理:將集合轉爲Set集合,再利用迭代器一一取出。
keySet():返回Map集合中所有的鍵,返回到set集合中,而Set中有迭代器,可以一一拿到各個鍵,再根據get(key)可以拿到鍵所對應的值。
entrySet():將map中的映射關係取出,即Map.Entry(key,value),返回到Set

示例1:學生(姓名和年齡相同視爲同一個人)作爲鍵,學生地址(String)作爲值存入Map集合,並取出。

class Student implements Comparable
{
    private String name;
    private int age;
    public Student(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    public int hashCode()
    {
        return name.hashCode()+age*37;
    }
    public boolean equals(Object obj)
    {
        if(Object instanceof Student)
            throw new ClassCastException("類型不匹配")
        Student s = (Student)obj;
        return this.name.equals(s.name)&&this.age==s.age
    }
    public int compareTo(Student s)
    {
        num = this.name.compareTo(s.name);
        if(num==0)
            return new Integer(this.age).compareTo(new Integer(s.age));
        return num;
    }
    public int getAge()
    {
        return this.age;
    }
    public int getName()
    {
        return this.name;
    }
    public String toString()
    {
        return this.name+":::"+this.age;
    }
}
public class MapTest
{
    public static void main(String args[])
    {
        HashMap hm = new HashMap();
        hm.add(new Student("lisi01",23),"北京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi03",25),"廣州");
        hm.add(new Student("lisi04",24),"南京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi01",26),"杭州");

        Set set = hm.keySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Person key = it.next();
            String value = hm.get(key);
            System.out.println(key.toString+"....."+value);
        }

        Set set2 = hm.entrySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Map.Entry entry = it.next();
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key.toString+"....."+value);
        }

    }
}

(二)Map的常用子類有三個:

1) HashTable:底層用的數據結構爲哈希表,是線程同步,不能存入空的鍵和值,jdk1.0,效率不高。
2) HashMap:底層用的數據結構爲哈希表,是線程不同步,能存入null的鍵和值,jdk1.2,可以替代HashTable,效率較高。
3)TreeMap:底層用的二叉樹數據結構,是線程不同步,能夠對鍵進行排序。
其實Map與Set很像,因爲Set底層實際上用的是Map。

示例2:利用TreeMap來實現學生按照年齡升序打印(指定比較器)。
首先定義比較器。

class MyComparator implements Comparator
{
    public int compare(Student s1,Student s2)
    {
        int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
        if(num==0)
            return s1.getName().compareTo(s2.getName());
        return num;
    }
}
public class MapTest
{
    public static void main(String args[])
    {
        HashMap hm = new HashMap(new MyComparator());
        hm.add(new Student("lisi01",23),"北京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi03",25),"廣州");
        hm.add(new Student("lisi04",24),"南京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi01",26),"杭州");

        Set set = hm.keySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Person key = it.next();
            String value = hm.get(key);
            System.out.println(key.toString+"....."+value);
        }

        Set set2 = hm.entrySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Map.Entry entry = it.next();
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key.toString+"....."+value);
        }
    }
}

示例3:“shfshfdlfhdoshfljeionfn”字符串中,每個字母出現的次數?打印格式爲a(1)b(4)…..

class StringTimeDemo
{
    public static void main(String args[])
    {
        String str = "shfshfdlfhdoshfljeionfn";
        TreeMap hm = new TreeMap();
        int num = 0;
        for(int i=0;i<str.length();i++)
        {
            Char ch = str.charAt(i);
            if(!(ch>'a'&&ch<'z'||ch>'A'&&ch<'Z'))
                continue;
            int value = hm.get(ch);
            if(hm!=null)
                num = value;
            num++;
            hm.put(ch,num);
            num = 0;
        }
        Set entry = hm.entrySet();
        Iterator it = entry.iterator();
        while(it.hasNext())
        {
            Map.Entry me = it.next();
            Char ch = me.getKey();
            int num = me.getValue();
            System.out.println(ch+"("+num+")");
        }
    }
}

此篇文章爲Map集合的簡單介紹!

發佈了32 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章