1. 程式人生 > 其它 >常用物件API(集和框架--Map)

常用物件API(集和框架--Map)

技術標籤:java

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.HashMap;
/**
 * Map:一次新增一對元素,Collection一次新增一個元素。
 *      Map也稱為雙列集和,Collection集和稱為單列集和。
 *      Map集和中儲存的就是鍵值對。
 *      Map集和中必須保證鍵的唯一性
 * 常用方法:
 *      1.新增
 *          value put(key,value);返回前一個和key關聯的值,如果沒有返回null
 *      2.刪除
 *          void clear();清空map集和
 *          value remove(key);根據指定的key刪除這個鍵值對
 *      3.判斷
 *          boolean containsKey(key);
 *          boolean contaionsValue(value);
 *          boolean isEmpty();
 *      4.獲取
 *          value get(key);通過鍵獲取值,如果沒有該鍵返回null
 *                          當然可以通過返回null來判斷是否包含該鍵
 *          int size();獲取鍵值對的個數。
 * Map常用子類:
 *      Hashtable:內部結構是雜湊表,是同步的,不允許null作為鍵,null作為值。
 *          Properties:用來儲存鍵值對型的配置檔案資訊。可以和io技術相結合。
 *      HashMap:內部結構是雜湊表,不是同步的,允許null作為鍵,null作為值。
 *      TreeMap:內部結構是二叉樹,不是同步的,可以對Map集和的鍵進行排序
 */
/** * 比較器 */ class ComparatorByName implements Comparator<Student>{ public int compare(Student s1,Student s2) { int temp = s1.name.compareTo(s2.name); return temp==0?s1.age-s2.age:temp; } } class Student implements Comparable{ String name; int age; Student(String name,
int age){ this.name = name; this.age = age; } public int compareTo(Object obj) { Student p = (Student)obj; if(this.age>p.age) return 1; else if(this.age<p.age) return -1; return this.name.compareTo(p.name);//為了在set存放物件時不因為年齡相同視為同一物件不儲存
// int temp = this.age-p.age; // return temp==0?this.name.compareTo(p.name):temp; } } public class packDemo{ public static void main(String[] args){ Map<Integer,String> map = new HashMap<Integer,String>(); TreeMapDemo(); } /* map基礎操作 */ public static void mapDemo(Map<Integer,String> map) {//學號和姓名 //1.新增元素 System.out.println(map.put(8, "kpp"));//null System.out.println(map.put(8, "zzz"));//kpp 存相同鍵,值會覆蓋 System.out.println(map.put(12, "qw")); map.put(2, "xy"); //2.刪除 System.out.println("remove:"+map.remove(12));//remove:qw //3.判斷 System.out.println("containsKey:"+map.containsKey(8));//containsKey:true System.out.println("containsValue:"+map.containsValue("xy"));//containsValue:true //4.獲取 System.out.println("get:"+map.get(2));//get:xy System.out.println(map);//{8=zzz} } /* 輸出map中每個鍵值對 */ public static void mapDemo2(Map<Integer,String> map){ map.put(8, "pp"); map.put(7, "zz"); map.put(12, "xx"); map.put(1, "mm"); //取出map中的所有元素 //原理:通過keySet方法獲取map中所有的鍵所在的Set集和,在通過Set的迭代器獲取到每一個鍵 //再對每一個鍵通過map集和的get方法獲取其對應的值即可。 Set<Integer> keySet = map.keySet(); Iterator<Integer> it = keySet.iterator(); while(it.hasNext()){ Integer key = it.next(); String value = map.get(key); System.out.println(key+"::"+value); } // 1::mm // 7::zz // 8::pp // 12::xx /* 通過map轉成Set就可以迭代。 找到了另一個方法。entrySet。 該方法將鍵和值的對映關係作為物件儲存到了Set集和中,而這個對映關係的型別就是Map.Entry型別(結婚證) */ Set<Map.Entry<Integer,String>> entrySet = map.entrySet(); Iterator<Map.Entry<Integer,String>> i = entrySet.iterator(); while(i.hasNext()){ Map.Entry<Integer,String> me = i.next(); Integer key = me.getKey(); String value = me.getValue(); System.out.println(key+"::"+value); } // 1::mm // 7::zz // 8::pp // 12::xx /* 方法values演示 */ Collection<String> values = map.values(); Iterator<String> i1 = values.iterator(); while(i1.hasNext()){ System.out.println(i1.next()); } // mm // zz // pp // xx } /* HashMap儲存自定義物件 不能排序 將學生物件和學生的歸屬地通過鍵與值儲存到map集和中。 */ public static void HashMapDemo_3(){ HashMap<Student,String> hm = new HashMap<Student,String>(); hm.put(new Student("ww", 25), "beijing"); hm.put(new Student("zz", 23), "sichuan"); hm.put(new Student("pp", 24), "sichuan"); hm.put(new Student("ss", 25), "chengdu"); Set<Student> keySet = hm.keySet(); Iterator<Student> it = keySet.iterator(); while(it.hasNext()){ Student stu = it.next(); String add = hm.get(stu); System.out.println(stu.name+":"+stu.age+"---"+add); } } // pp:24---sichuan // ss:25---chengdu // ww:25---beijing // zz:23---sichuan /* TreeMap儲存自定義物件 可以排序 */ public static void TreeMapDemo(){ // TreeMap<Student,String> tm = new TreeMap<Student,String>();//通過Student類的compareTo方法比較 TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());//通過比較器比較 tm.put(new Student("ww", 25), "beijing"); tm.put(new Student("zz", 23), "sichuan"); tm.put(new Student("pp", 24), "sichuan"); tm.put(new Student("ss", 25), "chengdu"); // Set<Student> keySet = tm.keySet(); // Iterator<Student> it = keySet.iterator(); Iterator<Map.Entry<Student,String>> it = tm.entrySet().iterator(); while(it.hasNext()){ // Student stu = it.next(); // String add = tm.get(stu); Map.Entry<Student,String> me = it.next(); String add = me.getValue(); Student stu = me.getKey(); System.out.println(stu.name+":"+stu.age+"---"+add); } } }