1. 继承Comparator接口,重写compare()方法
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.Random;
  8.  
  9. class Student implements Comparator<Student>{
  10. String name;
  11. int age;
  12. int id;
  13. public Student(){}
  14. public Student(String name,int age,int id)
  15. {
  16. this.name=name;
  17. this.age=age;
  18. this.id=id;
  19. }
  20. @Override
  21. public int compare(Student o1, Student o2) {
  22. // TODO Auto-generated method stub
  23. return o1.age-o2.age;
  24. }
  25. }
  26.  
  27. public class Test {
  28.  
  29. public static void main(String[] args) {
  30. Random rand=new Random();
  31. List<Student> list=new ArrayList<Student>();
  32. for(int i=0;i<20;i++)
  33. {
  34. Student ss=new Student("long-jing-wen-"+i,rand.nextInt(100),rand.nextInt(1000));
  35. list.add(ss);
  36.  
  37. }
  38.  
  39. Student student=new Student();
  40. Collections.sort(list, student);

  

  1. 继承Comparable,重写compareTo()方法
  2. package thread;
  3.  
  4. public class stu implements Comparable<stu>{
  5. public int id;
  6. public stu() {
  7.  
  8. }
  9. public stu(int id) {
  10.  
  11. this.id = id;
  12. }
  13.  
  14. public int getId() {
  15. return id;
  16. }
  17.  
  18. public void setId(int id) {
  19. this.id = id;
  20. }
  21.  
  22. @Override
  23. public String toString() {
  24. return "stu [id=" + id + "]";
  25. }
  26.  
  27. public int compareTo(stu o1, stu o2) {
  28. // TODO Auto-generated method stub
  29. return o1.id-o2.id;
  30. }
  31.  
  32. @Override
  33. public int compareTo(stu o) {
  34. // TODO Auto-generated method stub
  35. return this.id-o.id;
  36. }
  37.  
  38. }
  39.  
  40. Random rand=new Random();
  41. stu[] stu=new stu[20];
  42.  
  43. for(int i=0;i<20;i++)
  44. {
  45. stu ss2=new stu(rand.nextInt(100));
  46. stu[i]=ss2;
  47. }
  48.  
  49. Arrays.sort(stu);
  50.  
  51. for(int i=0;i<stu.length;i++)
  52. {
  53. System.out.print(stu[i]+" ");
  54. }
  55. System.out.println();

  

  1. Mapvalue排序
  2. HashMap<String, Long> map = new HashMap<String, Long>();
  3.  
  4. map.put("A", (long) 99);
  5. map.put("B", (long) 67);
  6. map.put("C", (long) 109);
  7. map.put("D", (long) 2);
  8.  
  9. System.out.println("unsorted map: " + map);
  10.  
  11. List<Map.Entry<String, Long>> list = new ArrayList<>(map.entrySet());
  12. Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
  13. public int compare(Map.Entry<String, Long> o1,
  14. Map.Entry<String, Long> o2) {
  15. return (int) (o2.getValue()-o1.getValue() );
  16. }
  17. });
  18.  
  19. System.out.println("results: " + list);

  

  1. Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  2. //遍历map中的键
  3. for (Integer key : map.keySet()) {
  4. System.out.println("Key = " + key);
  5. }
  6. //遍历map中的值
  7. for (Integer value : map.values()) {
  8. System.out.println("Value = " + value);
  9. }
  10.  
  11. 该方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。
  12.  
  13. 使用泛型:
  14. Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  15. Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
  16. while (entries.hasNext()) {
  17. Map.Entry<Integer, Integer> entry = entries.next();
  18. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  19. }
  20.  
  21. 不使用泛型:
  22. Map map = new HashMap();
  23. Iterator entries = map.entrySet().iterator();
  24. while (entries.hasNext()) {
  25. Map.Entry entry = (Map.Entry) entries.next();
  26. Integer key = (Integer)entry.getKey();
  27. Integer value = (Integer)entry.getValue();
  28. System.out.println("Key = " + key + ", Value = " + value);
  29. }
  30.  
  31. 最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
  32. Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  33. for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  34. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  35. }

  

实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历的更多相关文章

  1. Java中Comparator接口和Comparable接口的使用

    普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...

  2. comparator接口与Comparable接口的区别

    1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...

  3. 关于comparator接口和comparable接口以及它们各自的方法compare()和compareTo()

    在今天做的LeetCode的题中有两道都出现了利用接口实现对象的排序.两题的相关链接: 1.利用comparable接口对对象排序 2.利用comparator接口实现排序 因为之前都没接触过这两个接 ...

  4. Java Comparator方法 和 Comparable接口

    默认的排序方法: 让类继承Comparable接口,重写compareTo方法. 示例代码: package com.imooc.collection; import java.util.HashSe ...

  5. comparator接口与Comparable接口的差别

    1. Comparator 和 Comparable 同样的地方 他们都是java的一个接口, 而且是用来对自己定义的class比較大小的, 什么是自己定义class: 如 public class  ...

  6. Java 之 比较器( Comparator接口与 Comparable 接口)

    一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...

  7. Java集合中Comparator和Comparable接口的使用

    在Java集合中,如果要比较引用类型泛型的List,我们使用Comparator和Comparable两个接口. Comparable接口 -- 默认比较规则,可比较的 实现该接口表示:这个类的实例可 ...

  8. Java中 Comparator接口 与Comparable 的区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt159 comparator接口与Comparable接口的区别 1. Com ...

  9. Comparable 接口与Comparator的使用的对比

    package com.yhqtv.java; import org.junit.Test; import java.util.Arrays; import java.util.Comparator; ...

随机推荐

  1. 一些idea

    二分答案似乎和最小生成树有着不可描述的奇怪关系.(滑稽 联赛级别的在矩形上乱搞的题无非几个思路(按出现概率排序):建图,二维前缀和,dp 涉及到求合法区间数的问题往往要用到桶.等差数列等思想,或者尝试 ...

  2. 反向代理Reverse proxy

    https://www.zhihu.com/question/24723688/answer/160252724 反向代理在计算机世界里,由于单个服务器的处理客户端(用户)请求能力有一个极限,当用户的 ...

  3. svn 版本管理,trunk(主干),branch(分支),merge(合并)

    svn 版本管理,主要对trunk(主干).branch(分支).merge(合并)进行说明. svn作为一个常用的版本管理工具,一些基本操作必须要会,在这里整理一下自己使用svn的一些体会: svn ...

  4. RTTI RAII

    RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的 ...

  5. python- 粘包 struct,socketserver

    黏包 黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cmd.decode('utf-8'), sh ...

  6. Linux 版本查詢

    # uname -a 查看  Kernel版本 # cat /etc/redhat-release查看 linux版本(以RedHat為例) 1.核心查詢:uname -a結果:Linux 2.x.x ...

  7. 从一个url地址到最终页面渲染完成,发生了什么?

    从一个url地址到最终页面渲染完成,发生了什么? 1.DNS 解析 : 将域名地址解析为IP地址 浏览器DNS缓存 系统DNS缓存 路由器DNS缓存 网络运营商DNS缓存 递归搜索: www.baid ...

  8. css页面网址

    前端必看的文章 1.CSS设置居中的方案总结  https://juejin.im/post/5a7a9a545188257a892998ef 2.阮一峰老师的网站 http://www.ruanyi ...

  9. postgresql 取出分组中最大的几条数据

    WITH Name AS ( SELECT * FROM ( SELECT xzqdm, , ) xzdm, COUNT (*) sl FROM sddltb_qc WHERE xzqdm ') GR ...

  10. teb教程6

    代价地图的转换 简介:本部分关于怎样把代价地图转换插件应用到转换占据栅格costmap2d到几何形状来优化(测试阶段) teb_local_planner包支持costmap_converter插件, ...