TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置

package com.swift;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class Map_keySet_entrySet {

    public static void main(String[] args) {
        /*
         * TreeMap 集合存储自定义对象,并使用 2 中方式遍历获取
         */
        
        Map<Person,String> hm = new HashMap<Person, String>();
        hm.put(new Person("lisi",18), "加拿大");
        hm.put(new Person("zhangsa",17), "澳大利亚");
        hm.put(new Person("zhangsa",17), "澳大利亚");
        hm.put(new Person("wangwu",20), "新加坡");
        hm.put(new Person("zhaoliu",19), "新西兰");
        hm.put(new Person("zhaoliu",19), "新西兰");
        hm.put(new Person("lisa",22), "迪拜");
        
        //使用TreeMap无法排序比较是会出现Comparable错误 这时需要自己弄比较器
        Map<Person,String> tree = new TreeMap<Person, String>(new Comparator<Person>() {

            @Override
            public int compare(Person arg0, Person arg1) {
                int num=arg0.getAge()-arg1.getAge();
                return num==0?arg0.getName().compareTo(arg1.getName()):num;
            }
            
        });
        tree.put(new Person("lisisi",18), "china");
        
        //keySet方法一
        Set<Person> set=hm.keySet();
        for(Person per:set) {
            System.out.println("人员:"+per.toString()+"来自:"+hm.get(per));
        }
        
        Set<Person> set1=tree.keySet();
        for(Person per:set1) {
            System.out.println("人员:"+per.toString()+"来自:"+tree.get(per));
        }
        
        //entrySet方法二
        for(Map.Entry<Person, String> en:hm.entrySet()) {
            System.out.println();
            System.out.println("人员:"+en.getKey().toString()+"来自:"+en.getValue());
        }
        
        
        Map<Person,String> tree1 = new TreeMap<Person, String>(new Comparator<Person>() {

            @Override
            public int compare(Person arg0, Person arg1) {
                int num=arg0.getAge()-arg1.getAge();
                return num==0?arg0.getName().compareTo(arg1.getName()):num;
            }
            
        });
        tree1.put(new Person("fengqichanglin",18), "langyabang");
        System.out.println();
        Iterator<?> it=tree1.entrySet().iterator();
        while(it.hasNext()) {
            Entry<Person,String> entry=(Entry<Person, String>) it.next();
            System.out.println(entry.getKey().toString()+entry.getValue());
        }
        
        
    }

}

 


 

package sortmap_demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class CollectionsSort {

    public static void main(String[] args) {

        Map<Integer,User> map=new TreeMap<Integer,User>();
        map.put(1,new User("张三",28));
        map.put(2,new User("李四",29));
        map.put(3,new User("王五",30));
        map.put(4,new User("赵六",31));
        Set<Entry<Integer, User>> set = map.entrySet();
        List<Entry<Integer, User>> list = new ArrayList<Entry<Integer, User>>(set);
        Collections.sort(list,new Comparator<Entry<Integer, User>>() {

            @Override
            public int compare(Entry<Integer, User> o1, Entry<Integer, User> o2) {
                return o2.getValue().getAge()-o1.getValue().getAge();
            }
        });
        for (Entry<Integer, User> u : list) {
            System.out.println(u.getKey()+"--"+u.getValue());
        }
}
}

不颠倒key和value的位置,用list集合api排序的方法

TreeMap根据key的Comparator对象排序方法

package sortmap_demo;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class HashMapDemo {

    public static void main(String[] args) {

        Map<User,Integer> map=new TreeMap<User,Integer>(new Comparator<User>() {

            @Override
            public int compare(User o1, User o2) {
                if(o1.getAge()>o2.getAge()){
                    return -1;
                }else if(o1.getAge()==o2.getAge()){
                     return 0;
                }else{
                  return 1;
                }          
            }
        });
        map.put(new User("张三",28),1);
        map.put(new User("李四",29),2);
        map.put(new User("王五",30),3);
        map.put(new User("赵六",31),4);
        for (User u : map.keySet()) {
            System.out.println(u+"--"+map.get(u));
        }
}
}

 

posted @ 2018-01-20 18:51  Advancing-Swift  阅读(344)  评论(0编辑  收藏  举报