实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历
继承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遍历的更多相关文章
- Java中Comparator接口和Comparable接口的使用
普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...
- comparator接口与Comparable接口的区别
1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...
- 关于comparator接口和comparable接口以及它们各自的方法compare()和compareTo()
在今天做的LeetCode的题中有两道都出现了利用接口实现对象的排序.两题的相关链接: 1.利用comparable接口对对象排序 2.利用comparator接口实现排序 因为之前都没接触过这两个接 ...
- Java Comparator方法 和 Comparable接口
默认的排序方法: 让类继承Comparable接口,重写compareTo方法. 示例代码: package com.imooc.collection; import java.util.HashSe ...
- comparator接口与Comparable接口的差别
1. Comparator 和 Comparable 同样的地方 他们都是java的一个接口, 而且是用来对自己定义的class比較大小的, 什么是自己定义class: 如 public class ...
- Java 之 比较器( Comparator接口与 Comparable 接口)
一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...
- Java集合中Comparator和Comparable接口的使用
在Java集合中,如果要比较引用类型泛型的List,我们使用Comparator和Comparable两个接口. Comparable接口 -- 默认比较规则,可比较的 实现该接口表示:这个类的实例可 ...
- Java中 Comparator接口 与Comparable 的区别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt159 comparator接口与Comparable接口的区别 1. Com ...
- Comparable 接口与Comparator的使用的对比
package com.yhqtv.java; import org.junit.Test; import java.util.Arrays; import java.util.Comparator; ...
随机推荐
- JS基础入门篇(三十六)—面向对象( 三 )
1.class class es6 中 为 关键字用来声明 一个 类 1.只能通过new调用 2.不存在变量提升 3.多个方法之间直接书写,不需要,隔开 4.类的方法是直接定义在类的原型上的 5.定义 ...
- 【数据库】一篇文章搞掂:MySQL数据库
一.安装 使用版本:5.7(2018/08/03 阿里云的云数据库最高支持5.7,所以这里考虑用5.7) 下载版本:MySQL Community Server 5.7.23 下载地址:https:/ ...
- 20175213 2018-2019-2 《Java程序设计》第10周学习总结
Java内存模型 主内存与工作内存 Java内存模型主要目标:定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节.此处的变量(Variable)与Java编程中 ...
- flex 布局 实现三点筛子
实现麻将中三点筛子:效果如下图 具体实现代码: html代码: <div class="box"> <div class="item"> ...
- HTML-参考手册: HTML 事件属性
ylbtech-HTML-参考手册: HTML 事件属性 1.返回顶部 1. HTML 事件属性 全局事件属性 HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某 ...
- 这里ajax需要改成同步
var flag = true; var title = $("#modal").find("input[name=groupname]").val(); /* ...
- Visual Studio Code 修改字体
下载安装想要更换的字体,这里以 Fira Code 字体为例. Fira Code 字体的下载地址:https://github.com/tonsky/FiraCode 下载解压后安装字体,windo ...
- Django 模型层关系映射
一.一对一映射 1.什么是一对一 A表中的一条记录只能与B表中的一条记录相关联如:一夫一妻制 2.语法 允许在关联的两个类的任何一个类中 增加: 属性 = models.OneToOneField(E ...
- Tomcat启动脚本(1)startup.bat
@echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...
- 快速调通支付宝当面付Demo
1.访问如下地址: https://auth.alipay.com/login/ant_sso_index.htm?goto=https%3A%2F%2Fopenhome.alipay.com%2Fp ...