继承Comparator接口,重写compare()方法
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random; class Student implements Comparator<Student>{
String name;
int age;
int id;
public Student(){}
public Student(String name,int age,int id)
{
this.name=name;
this.age=age;
this.id=id;
}
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.age-o2.age;
}
} public class Test { public static void main(String[] args) {
Random rand=new Random();
List<Student> list=new ArrayList<Student>();
for(int i=0;i<20;i++)
{
Student ss=new Student("long-jing-wen-"+i,rand.nextInt(100),rand.nextInt(1000));
list.add(ss); } Student student=new Student();
Collections.sort(list, student);

  

继承Comparable,重写compareTo()方法
package thread; public class stu implements Comparable<stu>{
public int id;
public stu() { }
public stu(int id) { this.id = id;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Override
public String toString() {
return "stu [id=" + id + "]";
} public int compareTo(stu o1, stu o2) {
// TODO Auto-generated method stub
return o1.id-o2.id;
} @Override
public int compareTo(stu o) {
// TODO Auto-generated method stub
return this.id-o.id;
} } Random rand=new Random();
stu[] stu=new stu[20]; for(int i=0;i<20;i++)
{
stu ss2=new stu(rand.nextInt(100));
stu[i]=ss2;
} Arrays.sort(stu); for(int i=0;i<stu.length;i++)
{
System.out.print(stu[i]+" ");
}
System.out.println();

  

Map按value排序
HashMap<String, Long> map = new HashMap<String, Long>(); map.put("A", (long) 99);
map.put("B", (long) 67);
map.put("C", (long) 109);
map.put("D", (long) 2); System.out.println("unsorted map: " + map); List<Map.Entry<String, Long>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
public int compare(Map.Entry<String, Long> o1,
Map.Entry<String, Long> o2) {
return (int) (o2.getValue()-o1.getValue() );
}
}); System.out.println("results: " + list);

  

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//遍历map中的键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
} 该方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。 使用泛型:
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
} 不使用泛型:
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
} 最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

  

实现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的骚操作

    1.var声明 "jack".var ==>final String name = "jack"; 2.null判空 user.null==>if( ...

  2. linux下lamp.sh一键配置lamp环境流程

    linux下lamp.sh一键配置lamp环境流程 一.总结 一句话总结: 2.将网站从github上clone到/data/www/网站域名/ 3.更改网站目录权限:chown -R apache: ...

  3. MySQL 计算中位数

    mysql中位数 奇数取中间的值,偶数取中间两个数的平均值 eg: 12345 中位数4 1234 中位数2.5 SELECT avg(t1.money) as median_val FROM ( S ...

  4. 58、salesforce学习笔记(五)

    Set集合 Set<String> set1 = new Set<String>(); set1.add('1'); set1.add('2'); Set<String& ...

  5. 畜禽免疫系统使用LODOP打印

    <div class="btn_box"> <asp:Button ID="btnPrint" Text="预览并打印" ...

  6. CSS3 resize 属性

    CSS3 resize 属性 CSS 参考手册 实例 规定可以由用户调整 div 元素的大小: div { resize:both; overflow:auto; } 支持 Firefox 4+.Ch ...

  7. Linux下安装JDK(小白教程)

    一.      选择与下载jdk 1. 官网上按照自己的系统版本下载相应jdk,因为我的LINUX(testbest)是32位的,所以我下载32位的jdk. 2. 官网下载地址:http://www. ...

  8. Cocos2d-x之Scene

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. Scene场景也是cocos2dx中必不可少的元素,游戏中通常我们需要构建不同的场景(至少一个),游戏里关卡.版块的切换也就是一个一个场景 ...

  9. zabbix cpu监控介绍

    一.CPU utilization 使用Zabbix查看CPU利用率,会有下面几个值: CPU idle time:空闲的cpu时间比[简称id]CPU user time:用户态使用的cpu时间比[ ...

  10. springboot Service层单元测试

    两个实现类实现同一个Service接口 public interface CustomUrlService { List<ShopMetrics> getShopMetrics(); } ...