java集合架構---Map總結

來源:互聯網
上載者:User

標籤:集合   map   java   

/*Map--    |--Hashtable:底層是雜湊表資料結構,不可以存入null鍵null值。線程同步。    |--HashMap:底層是雜湊表資料結構,可以存入null鍵null值。線程不同步。    |--TreeMap:底層是二叉樹資料結構,線程不同步。可以給鍵排序 */package pack;import java.util.Comparator;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TreeMap;/*public class Main {    public static void main(String[] args) {        Map<Integer,String> map = new HashMap<Integer,String>();        map.put(1, "java1");  //增        map.put(2, "java2");        map.put(3, "java3");        map.remove(4);  //刪        //sys(map.containsKey(1));  //判        //sys(map.get(2));      //查        //sys(map);        查方法一:通過Set集合中的迭代器得到鍵,再通過get方法得到值        Set<Integer> s = map.keySet();        for(Iterator<Integer> it = s.iterator();it.hasNext(); ) {            sys(map.get(it.next()));          }        Collection coll = map.values();        for(Iterator<Integer> it = coll.iterator();it.hasNext(); ) {            sys(it.next());        }        sys("---------------");        查方法二:將Map集合中的映射關係取出,存到Set集合中        Set<Map.Entry<Integer, String>> entryset = map.entrySet();        Iterator<Map.Entry<Integer, String>> it = entryset.iterator();        while(it.hasNext()) {            Map.Entry<Integer, String> me = it.next();            sys(me.getValue());            sys(me.getKey());        }    }    public static void sys(Object obj) {        System.out.println(obj);    }}*//*在TreeSet中傳入對象參數,按年齡排序*/public class Main {    public static void main(String[] args) {        TreeMap<Person,Integer> ts = new TreeMap<Person,Integer>(new MyComparator());        ts.put(new Person("java1",1), 01);        ts.put(new Person("java3",3), 03);        ts.put(new Person("java2",2), 02);        ts.put(new Person("java4",4), 04);        Set<Map.Entry<Person,Integer>> entryset = ts.entrySet();        Iterator<Map.Entry<Person,Integer>> it = entryset.iterator();        while(it.hasNext()) {            Map.Entry<Person,Integer> me = it.next();            Person per = me.getKey();            Integer age1 = me.getValue();            sys(per+"..."+age1);        }    }    public static void sys(Object obj) {        System.out.println(obj);    }}class Person {    private String name;    private int age;    Person(String name,int age) {        this.name = name;        this.age = age;    }    public int getAge() {        return age;    }    public String getName() {        return name;    }}class MyComparator implements Comparator<Person> {    public int compare(Person p1,Person p2) {        int a = new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));        if(a==0) {            //比較與前面行的區別,Integer與String有compareTo方法,而int沒有            return p1.getName().compareTo(p2.getName());          }        return a;    }}

java集合架構---Map總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.