十五、Collections.sort(<T>, new Comparator<T>() {})针对字符串排序
1、排序对象全是字母组成,可以根据ASCII编码表排序
package com.abcd;
public class Person{
private String name;
private int age;
private int salary;
public Person() {
}
public Person(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
2、排序测试代码
package com.abcd; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class PersonTest {
public static void main(String[] args){
List<Person> people = new ArrayList<>();
people.add(new Person("ZZZ",20,100));
people.add(new Person("aaa",48,109));
people.add(new Person("aa",30,109)); System.out.println(people);
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) { return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase()); }
});
System.out.println(people);
}
}
int comparaTo( )方法底层源码:
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
//首先的比较规则,比较首字母,依次第二位字母,第三位字母等逐个比较
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
//然后字母比较完了后,比较字符串的长度
return len1 - len2;
}
3、运行结果
[Person{name='ZZZ', age=20, salary=100}, Person{name='aaa', age=48, salary=109}, Person{name='aa', age=30, salary=109}]
[Person{name='aa', age=30, salary=109}, Person{name='aaa', age=48, salary=109}, Person{name='ZZZ', age=20, salary=100}]
2、排序目标全是中文,实现首字母排序
package com.abcd; import java.text.Collator;
import java.util.*; public class PersonTest {
public static void main(String[] args){
List<Person> people = new ArrayList<>();
people.add(new Person("博泰",20,100));
people.add(new Person("阿明",48,109));
people.add(new Person("啊",30,109)); System.out.println(people);
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) { Comparator cmp = (Collator.getInstance(Locale.CHINA));
return cmp.compare(o1.getName(), o2.getName()); }
});
System.out.println(people);
}
}
运行结果:
[Person{name='博泰', age=20, salary=100}, Person{name='阿明', age=48, salary=109}, Person{name='啊', age=30, salary=109}]
[Person{name='啊', age=30, salary=109}, Person{name='阿明', age=48, salary=109}, Person{name='博泰', age=20, salary=100}]
十五、Collections.sort(<T>, new Comparator<T>() {})针对字符串排序的更多相关文章
- Collections.sort(List<T> Comparator) 自定义排序
Collections.sort(basicinfoList, new Comparator<MlisBasicinfo>() { @Override public int compare ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- 015——数组(十五)sort natsort shuffle natcasesoft array_multisort
<?php /*数组排序函数 * sort natsort shuffle natcasesoft array_multisort */ //sort() 对数组元素进行递增的排序, /*$ar ...
- Python学习日记(十五) collections模块
在内置函数(dict.list.set.tuple)的基础上,collections模块还提供了几个其他的数据类型:Counter.deque.defaultdict.namedtuple和Order ...
- 关于java Collections.sort 排序
public static void main(String[] args) { int[] dd = {12,34,46,123,23,2,35,13,543231,65,5645,57}; Arr ...
- Java提高十五:容器元素比较Comparable&Comparator深入分析
我们经常用容器来存放元素,通常而言我们是不关系容器中的元素是否有序,但有些场景可能要求容器中的元素是有序的,这个时候用ArrayList LinkedList Hashtable HashMap ...
- Java—集合框架 Collections.sort()、Comparable接口和Comparator接口
Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...
- ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
package com.iotek.set; import java.util.ArrayList; import java.util.Collections; import java.util.Co ...
- Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用
摘要:本文主要介绍Java8 中Arrays.sort()及Collections.sort()中Lambda表达式及增强版Comparator的使用. 不废话直接上代码 import com.goo ...
随机推荐
- C语言数据结构基础学习笔记——图
图(G)由顶点集(V)和边集(E)组成,G=(V,E) 常用概念: ①V(G)表示图G中顶点的有限非空集,V永不为空: ②用|V|表示图G中顶点的个数,也称为图G的阶: ③E(G)表示图G中顶点之间关 ...
- js实现输入某串数字,构建完全二叉树,并判断是否为二叉搜索树
思路:若为二叉搜索树,则中序遍历为递增的 let arr = [15,8,16,6,10];let pindex = [];function Node(){ this.root = null; thi ...
- 使用JavaScript制作页面特效2
1.Date对象的常用方法 setFullYear() setMonth() setDate() setHours() setMinutes() setSeconds() 定时函数 setTimeou ...
- 域名动态解析到动态IP
一般宽带用户的IP都是动态IP,重连之后IP可能会发生变化. 如果想在其他地方连接家里的设备,或者在家中搭建服务器,就会受到影响. 现在提供一种动态解析域名的方式,只要检测到IP的变化,那么就调用阿里 ...
- tomcat启动成功后访问却404
1.检查是否把项目添加进tomcat,好久不用tomcat这次就犯了这种低级错误 2.检查路径,tomcat中的访问路径与项目中设置的路径是否一样,因为这次有些配置文件直接复制的源码,但源码中项目名称 ...
- java异常——五个关键字(try、catch、finally、throw、throws)
一.try.catch.finally常用组合 try{ xxx }catch(xxxException e){ e.printStackTrace(); } try{ xxx }catch(xxxE ...
- Python第10天
装饰器:本质上是函数,为其他函数添加附件功能. 装饰器 = 高阶函数 + 函数嵌套 + 闭包 原则(开放封闭原则):1,不修改被修饰函数代码.2,不修改被修饰函数调用方式. @方法名
- 关于JVM加载内存图学习小密招
先看如下代码: Person.java public class Person { private String name; private int age; static int count = 0 ...
- 序列号多个input输入demo
<input class="inputs" type="text" maxlength="4" /> <input cla ...
- DEDECMS 多站用一个站图片
function replaceurl($newurl) { $newurl=str_replace('src="/uploads/allimg/','src="xxx.com/u ...