继承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. 《ArcGIS Runtime SDK for .NET开发笔记》 --Hello Word

    这里我们将创建第一个用于显示地图的APP. 1.新建一个WPF程序 首先我们打开Visual Studio,选择新建项目. 选择已安装——模板——Windows桌面——WPF应用程序  2.添加Run ...

  2. 15. Django连接Mysql数据库

    安装PyMySQL,打开cmd, pip install PyMySQL 在.../blog/init.py 目录下添加: import pymysql pymysql.install_as_MySQ ...

  3. 【狼】狼的unity3d脚本学习

      记录学习中的问题,时刻更新 unity获取鼠标所在位置 BOOL GetCursorPos(   LPPOINT lpPoint); 获取鼠标所在位置,不过原点在左下角 ///////////// ...

  4. Rust <4>:所有权、借用、切片

    tips:栈内存分配大小固定,访问时不需要额外的寻址动作,故其速度快于堆内存分配与访问. rust 所有权规则: 每一个值在任意时刻都有且只有唯一一个所有者 当所有者离开作用域时,这个值将被丢弃 所有 ...

  5. shell编程:字符串练习题string.sh

    string.sh脚本 #!/bin/bash # string="Bigdata process framework is Hadoop,Hadoop is an open source ...

  6. docker对容器进行资源限制

    对内存进行资源限制 docker run --memory=200m soymilk/stress 对cpu进行资源限制 终端1 docker run --name=test1 --cpu-share ...

  7. Vue篇1

    2019 Vue 面试题汇总(持续更新中...) 1. vue中的MVVM模式 即Model-View-ViewModel. Vue是以数据为驱动的,Vue自身将DOM和数据进行绑定,一旦创建绑定,D ...

  8. vue.js实现用户评论、登录、注册、及修改信息功能

    vue.js实现用户评论.登录.注册.及修改信息功能:https://www.jb51.net/article/113799.htm

  9. this与super的语法比较

    this 代表当前对象 可以代表当前属性,当前方法,当前对象(整个自己). 作用:解决同名变量的同名问题,同明变量可能来源于父类,局部变量和成员变量... 语法使用:this( 实参... ); 调用 ...

  10. python基础之数据类型初始

    变量 贴标签 变量名规则:只能是字母下划线和数字组成,不能以数字开头,不能和关键字同名,不能使用拼音和中文,具有可描述性,区别大小写,变量名推荐写法:驼峰提,下划线(官方推荐) 变量赋值 常量 特性: ...