• 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));
            }
    }
    }
  • 相关阅读:
    idea-----Intellij IDEA配置tomcat(非maven项目)
    idea-----idea的项目中output框出现乱码
    mysql on windows的安装
    maven配置
    安装tomcat8.5
    jdk11.0.2安装
    idea创建maven web项目
    Mac下使用sshpass让iterm2支持多ssh登录信息保存
    iterm 2快捷键
    java 8 Base64用法
  • 原文地址:https://www.cnblogs.com/qingyundian/p/8321627.html
Copyright © 2020-2023  润新知