继承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. div绝对居中、宽高自适应、多栏宽度自适应

    <style> body { background-color: #e1ddd9; font-size: 12px; font-family: Verdana, Arial, Helvet ...

  2. python模块学习之HTMLTestRunner模块生成HTML测试报告

    #!/usr/bin/env python #-*- coding:utf-8 -*- from HTMLTestRunner import HTMLTestRunner import time im ...

  3. 【C++第一个Demo】---控制台RPG游戏4【角色系统】

    [角色基类] #ifndef _ROLE_H_ #define _ROLE_H_ #include<list> #include<vector> #include " ...

  4. centos安装virtualbox

    参考:http://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-r ...

  5. tdom中selectNodes的使用

    tdom中selectNodes的使用 */--> pre.src {background-color: #002b36; color: #839496;} pre.src {backgroun ...

  6. 常用Javascript方法

    一,检测是否是Array 1,通过constructor检测 function isArray(value){ return value && typeof value === 'ob ...

  7. 深信服杯ctf部分wp

    CRYPTO1,NO SOS题目给了一段由.和-构成的密码由于题目提示不是摩斯码,将.和-化为0和1,长度为65位无法与8或7整除,无法转换为ascii,但可以被5整除,猜测为培根密码,将0化为a,1 ...

  8. docker部署一个简单的mian.py项目文件

    安装docker yum install -y docker  启动docker systemctl start docker   查询可安装的Python版本,默认centos python 2.7 ...

  9. echarts 给formatter文字添加不同颜色

    legend: { x : 'center', y : 'bottom', icon: "circle", itemWidth: 8, // 设置宽度 itemHeight: 8, ...

  10. linux下如何挂载磁盘

    1.显示磁盘使用情况:#df 2.显示磁盘:#fdisk -l 3.格式化分区:#mkfs.ext4 /dev/vdb1           //注:将/dev/vdb1格式化为ext4类型(文件类型 ...