博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中对map按key或val排序
阅读量:4156 次
发布时间:2019-05-25

本文共 3224 字,大约阅读时间需要 10 分钟。

首先先看下Java中的Collections.sort()排序方法:

Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式:

    public static 
> void sort(List
 list) {        list.sort(null);    }
    public static 
 void sort(List
 list, Comparator
 c) {        list.sort(c);    }

通过实现Comparator接口的compare方法来完成自定义排序

Comparator 的使用有两种方式:

Collections.sort(list,Comparator<T>);

list.sort(Comparator<T>);

其实主要是看 Comparator 接口的实现,重写里面的 compare 方法。代码如下:

1

2

3

4

5

6

7

//自定义排序1

Collections.sort(list, new Comparator<Student>() {

    @Override

    public int compare(Student o1, Student o2) {

        return o1.getId() - o2.getId();

    }

});

 

compare(Student o1, Student o2) 方法的返回值跟 Comparable<> 接口中的 compareTo(Student o) 方法 返回值意思相同。另一种写法如下:

1

2

3

4

5

6

7

//自定义排序2

list.sort(new Comparator<Student>() {

    @Override

    public int compare(Student o1, Student o2) {

        return o1.getId() - o2.getId();

    }

});

 

 

 

根据Map<key, val>中的key排序map,排序完成后放进linkedHashMap中,也可以放在List<对象>中,因为map的话,返回到前端顺序会乱。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

    /**

     * 按key排序(sort by key).

     

     * @param oriMap 要排序的map集合

     * @param isAsc(true:升序,false:降序)

     * @return

     */

    private Map<String, Long> sortMapByKey(Map<String, Long> oriMap, final boolean isAsc) {  

        Map<String, Long> sortedMap = new LinkedHashMap<String, Long>();  

        if (oriMap != null && !oriMap.isEmpty()) {  

            List<Map.Entry<String, Long>> entryList = new ArrayList<Map.Entry<String, Long>>(oriMap.entrySet());  

            Collections.sort(entryList,  

                    new Comparator<Map.Entry<String, Long>>() {  

                        public int compare(Entry<String, Long> entry1,  

                                Entry<String, Long> entry2) {  

  

                            String key1 = entry1.getKey();

                            String key2 = entry2.getKey();

                             

                            // 判定

                            int rst = 0;

                            if (isAsc) {

                                rst = key1.compareTo(key2);

                            else {

                                rst = key2.compareTo(key1);

                            }

                            return rst; //1大于;0等于;-1小于

                        }  

                    });  

            Iterator<Map.Entry<String, Long>> iter = entryList.iterator();  

            Map.Entry<String, Long> tmpEntry = null;

            while (iter.hasNext()) {  

                tmpEntry = iter.next();  

                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());  

            }  

        }  

        return sortedMap; 

    }

根据val排序

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

    /**

     * 按值排序(sort by value).

     

     * @param oriMap 要排序的map集合

     * @param isAsc(true:升序,false:降序)

     * @return

     */

    private Map<String, Long> sortMapByValueLong(Map<String, Long> oriMap, final boolean isAsc) {  

        Map<String, Long> sortedMap = new LinkedHashMap<String, Long>();  

        if (oriMap != null && !oriMap.isEmpty()) {  

            List<Map.Entry<String, Long>> entryList = new ArrayList<Map.Entry<String, Long>>(oriMap.entrySet());  

            Collections.sort(entryList,  

                    new Comparator<Map.Entry<String, Long>>() {  

                        public int compare(Entry<String, Long> entry1,  

                                Entry<String, Long> entry2) {  

                            long value1 = 0, value2 = 0;  

                            try {  

                                value1 = entry1.getValue();

                                value2 = entry2.getValue();

                            catch (NumberFormatException e) {  

                                value1 = 0;  

                                value2 = 0;  

                            

                            // 判定

                            long rst = 0;

                            if (isAsc) {

                                rst = value1 - value2;

                            else {

                                rst = value2 - value1;

                            }

                            return (int)rst;

                        }  

                    });  

            Iterator<Map.Entry<String, Long>> iter = entryList.iterator();  

            Map.Entry<String, Long> tmpEntry = null;

            while (iter.hasNext()) {  

                tmpEntry = iter.next();  

                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());  

            }  

        }  

        return sortedMap; 

    }

 

转载地址:http://ehwxi.baihongyu.com/

你可能感兴趣的文章
docker清理命令
查看>>
SHELL编程
查看>>
shell中的判断表达式
查看>>
shell中字符串操作
查看>>
shell expect交互
查看>>
卸载nginx 并重新安装
查看>>
与docker容器交互
查看>>
配置带用户权限的docker registry v2
查看>>
Java NIO之Selector
查看>>
CentOS7下安装MySQL5.7安装与配置
查看>>
VM虚拟机中Linux扩展磁盘空间的方法
查看>>
linux修改磁盘挂载目录
查看>>
J2SE发送http请求并获取返回数据
查看>>
jfreechart饼图和柱状图ajax
查看>>
Centos新建永久网桥
查看>>
js滚动图片新闻
查看>>
js tab标签页
查看>>
js滚动新闻
查看>>
pig简介
查看>>
pig自定义函数
查看>>