JAVA學習筆記(二十二)- 集合HashMap與Hashtable,hashmaphashtable

來源:互聯網
上載者:User

JAVA學習筆記(二十二)- 集合HashMap與Hashtable,hashmaphashtable
HashMap類

import java.util.HashMap;import java.util.Map;/* * HashMap類,實現Map介面,以索引值對的形式儲存資料key-value */public class Test04{    public static void main(String[] args)    {        // 建立一個HashMap集合        Map map = new HashMap();        // 向集合中儲存索引值對資料        map.put("one", "tom");        map.put("two", "mike");        map.put("three", "alice");        //key必須唯一,如果key相同,會更新相應的值        map.put("three", "jack");        map.put("four", "alex");        //根據key取出其對應的值value        Object obj=map.get("two");        System.out.println("two:"+obj);        //根據key刪除指定的索引值對        System.out.println(map.remove("four"));        //判斷是否存在指定的key或value        System.out.println("key為four的元素存在嗎?"+map.containsKey("four"));        System.out.println("value為alice的元素存在嗎?"+map.containsValue("jack"));        System.out.println("集合是否為空白?"+map.isEmpty());        System.out.println("集合中元素的個數:"+map.size());        System.out.println("HashMap集合中的元素:"+map);    }}
迭代Map集合
import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;/* * 迭代Map集合 */public class Test05{    public static void main(String[] args)    {        // 1.建立一個HashMap集合,儲存學生資訊        Map students = new HashMap();        // 2.建立學生對象        Student stu1 = new Student(9527, "唐伯虎", 20);        Student stu2 = new Student(3306, "唐伯龍", 22);        Student stu3 = new Student(1521, "唐伯豬", 18);        // 3.將學生添加到集合中,學號作為key,學生對象作為value        students.put(stu1.getId(), stu1);        students.put(stu2.getId(), stu2);        students.put(stu3.getId(), stu3);        // 輸出學生資訊        System.out.println("共有" + students.size() + "個學生!");        System.out.println("分別為:");        // 1.遍曆集合中的key        Set keys = students.keySet();// 擷取key的集合        System.out.println("學號\t姓名\t年齡");        System.out.println("***************遍曆key方法一*******************");        for (Object key : keys)// 遍曆所有的key        {            Student stu = (Student) students.get(key); // 根據key取出對應的value            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"                    + stu.getAge());        }        System.out.println("***************遍曆key方法二*******************");        Iterator it = keys.iterator();// 擷取key集合對應的迭代器        while (it.hasNext())        {            Student stu = (Student) students.get(it.next()); // 根據key取出對應的value            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"                    + stu.getAge());        }        // 2.遍曆集合中的value        Collection c = students.values();// 擷取value的集合        System.out.println("***************遍曆value方法一*******************");        for (Object obj : c)        {            Student stu = (Student) obj;// 擷取value,即學生對象            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"                    + stu.getAge());        }        System.out.println("***************遍曆value方法二*******************");        Iterator it2 = c.iterator();        while (it2.hasNext())        {            Student stu = (Student) it2.next();            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"                    + stu.getAge());        }        // 3.直接遍曆集合中的key-value整體        Set set=students.entrySet();        System.out.println("***************遍曆entry(key-value)方法一*******************");        for(Object obj:set){            Map.Entry entry=(Map.Entry) obj;            int id=(Integer) entry.getKey();//通過Map.Entry的getKey()方法擷取key            Student stu=(Student) entry.getValue();//通過Map.Entry的getValue()方法擷取value            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"                    + stu.getAge());        }        System.out.println("***************遍曆entry(key-value)方法二*******************");        Iterator it3=set.iterator();        while(it3.hasNext()){            Map.Entry entry=(Map.Entry) it3.next();            int id=(Integer) entry.getKey();            Student stu=(Student) entry.getValue();            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"                    + stu.getAge());        }    }}/* * 學生 */class Student{    private int id;// 學號    private String name;// 姓名    private int age;// 年齡    public Student(int id, String name, int age)    {        super();        this.id = id;        this.name = name;        this.age = age;    }    public int getId()    {        return id;    }    public void setId(int id)    {        this.id = id;    }    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;    }}
Hashtable類
import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Set;/* * Hashtable類,實現Map介面,用法類似於HashMap */public class Test06{    public static void main(String[] args)    {        Hashtable ht = new Hashtable();        //向Hashtable中存入資料        ht.put(1, "李玉章");        ht.put(5, "萬麗慶");        ht.put(25, "陳程");        //ht.put(null, "吳偉");//Hashtable的key和value都不允許為null        HashMap hm=new HashMap();        hm.put(null, "葛軍");//HashMap的key和value都可以為空白,但key必須唯一        hm.put(13, null);        System.out.println(hm);        //從Hashtable中擷取資料        String name=(String) ht.get(5);        System.out.println("key:5\nvalue:"+name);        System.out.println(ht);        /*         * 遍曆key的集合         */        System.out.println("***************使用keySet()方法,通過foreach迴圈,遍曆key的集合");        Set set=ht.keySet();        for(Object obj:set){            int id=(Integer) obj;            String n=(String) ht.get(id);            System.out.println(id+","+n);        }        /*         * 使用Enumeration迭代         */        System.out.println("***************使用elements()方法,通過Enumeration,遍曆value的集合");        Enumeration e=ht.elements();//遍曆value的Enumeration執行個體        while(e.hasMoreElements()){//判斷是否有元素            Object obj=e.nextElement();//擷取當前元素,即當前的value            System.out.println(obj);        }        System.out.println("***************使用keys()方法,通過Enumeration,遍曆key的集合");        Enumeration e2=ht.keys();        while(e2.hasMoreElements()){            int id=(Integer) e2.nextElement();//擷取當前的key            String n=(String) ht.get(id);            System.out.println(id+","+n);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.