TreeMap 排序

一、TreeMap

TreeMap 默认排序规则:按照key的字典顺序来排序(升序)

当然,也可以自定义排序规则:要实现Comparator接口。

用法简单,先看下下面的demo

public class SortDemo {

    public static void main(String[] args) {
        System.out.println("---------------- 默认 排序结果-----------------");
        createDefaultSortTreeMap();
        System.out.println("---------------- 自定义 排序结果-----------------");
        createDefinitionSortTreeMap();
    }
    
    public static void createDefaultSortTreeMap() {
        TreeMap<String, String> map = new TreeMap<String, String>();
        
        init(map);
        print(map);
    }
    
    public static void createDefinitionSortTreeMap() {
        
        TreeMap<String, String> map = new TreeMap<String, String>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                    return o2.compareTo(o1);
            }
            
        });
        
        init(map);
        print(map);
    }
    
    public static void init(Map<String, String> map) {
        map.put("c", "1");
        map.put("a", "1");
        map.put("bb", "1");
        map.put("b", "1");
    }
    
    public static void print(Map<String, String> map) {
        Iterator<Entry<String, String>> it = map.entrySet().iterator();
        while(it.hasNext()) {
            Entry<String, String> entry = it.next();
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }

结果:
---------------- 默认 排序结果-----------------
a : 1
b : 1
bb : 1
c : 1
---------------- 自定义 排序结果-----------------
c : 1
bb : 1
b : 1
a : 1

二、扩展:字典顺序

1、排序规则

 两个字符串 s1, s2比较

(1)、如果s1和s2是父子串关系,则 子串 < 父串

(2)、如果非为父子串关系, 则从第一个非相同字符来比较。

     例子 s1 = "ab", s2 = "ac"    这种情况算法规则是从第二个字符开始比较,由于'b' < 'c' 所以  "ab" < "ac"

(3)、字符间的比较,是按照字符的字节码(ascii)来比较

2、  compareTo 实现机制:对于字符串来说,字典排序规则;对于数字来说,直接按照大小排序

下面, 是我在项目中,遇到的一个坑,也不能算坑吧,只能说基础掌握得不扎实,导致老不断犯错。先说下场景,有个需求要对Map排序,当时想当然就用了自定义的TreeMap(new

Comparator )

key 为 String, value 也会String类型, 然后很不幸的是,我的Key 是 数字 字符串 ,如 Map.put("2","1"),Map.put("12","1"),Map.put("13","1")

正常思维排序结果是 "2" < "12" < "13" ,仔细一想,compareTo 底层算法是 "字典排序",正确的排序结果 : "12" < "13" <  "2" 

但是我的需求又是想要"2" < "12" < "13"这种效果,如何实现呢?很简单,把Key改为Long类型,这样,就会按照大小来排序。

看下下面的例子,可能比较简单明了!

 

public class SortDemo2 {
    
    private final static int SIZE = 30;

    public static void main(String[] args) {
        System.out.println("---------------- key 为 Sting 排序结果-----------------");
        String s = new String();
        createTreeMap(s);
        System.out.println("---------------- key 为 Long 排序结果-----------------");
        Long l = new Long(0);
        createTreeMap(l);
    }
    
    public static void createTreeMap(Object obj) {
        
        TreeMap<Object, Object> map = new TreeMap<>(new Comparator<Object>() {

            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof String && o2 instanceof String) {
                    return ((String) o1).compareTo((String) o2);
                } else if(o1 instanceof Long && o2 instanceof Long) {
                    return ((Long) o1).compareTo((Long) o2);
                }
                return 0;
            }
            
        });
        
        for(int i = 1; i<SIZE; i++) {
            if(obj instanceof String) {
                map.put(String.valueOf(i), String.valueOf(i));
            }
            if(obj instanceof Long) {
                map.put(Long.valueOf(i), Long.valueOf(i));
            }
        }
        
        print(map);
    }
    
    public static void print(Map<Object, Object> map) {
        Iterator<Entry<Object, Object>> it = map.entrySet().iterator();
        while(it.hasNext()) {
            Entry<Object, Object> entry = it.next();
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

结果:

---------------- key 为 Sting 排序结果-----------------
1 : 1
10 : 10
11 : 11
12 : 12
13 : 13
14 : 14
15 : 15
16 : 16
17 : 17
18 : 18
19 : 19
2 : 2
20 : 20
21 : 21
22 : 22
23 : 23
24 : 24
25 : 25
26 : 26
27 : 27
28 : 28
29 : 29
3 : 3
4 : 4
5 : 5
6 : 6
7 : 7
8 : 8
9 : 9
---------------- key 为 Long 排序结果-----------------
1 : 1
2 : 2
3 : 3
4 : 4
5 : 5
6 : 6
7 : 7
8 : 8
9 : 9
10 : 10
11 : 11
12 : 12
13 : 13
14 : 14
15 : 15
16 : 16
17 : 17
18 : 18
19 : 19
20 : 20
21 : 21
22 : 22
23 : 23
24 : 24
25 : 25
26 : 26
27 : 27
28 : 28
29 : 29

 

转载出处:https://www.cnblogs.com/chenmo-xpw/p/4922641.html

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值