java Collections.sort()实现List排序的默认方法和自定义方法
1.java提供的默认list排序方法
主要代码:
List
list.add("王硕");
list.add("李明");
list.add("刘迪");
list.add("刘布");
//升序
Collections.sort(list,Collator.getInstance(java.util.Locale.CHINA));//注意:是根据的汉字的拼音的字母排序的,而不是根据汉字一般的排序方法
for(int i=0;i
System.out.print(list.get(i));
}
System.out.println("");
//降序
Collections.reverse(list);//不指定排序规则时,也是按照字母的来排序的
for(int i=0;i
System.out.print(list.get(i));
}
输出结果:
李明刘布刘迪刘媛媛王硕
王硕刘媛媛刘迪刘布李明
2.自定义的排序规则:
第一种是model类实现Comparable接口,重写重写int compareTo(Object o)方法
model类:
public class StudentDTO implements Comparable
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public ObjType getType()
{
return type;
}
public void setAge(int age)
{
this.age= age;
}
@Override
public int compareTo(Object o)
{
StudentDTO sdto = (StudentDTO)o;
int otherAge = sdto.getAge();
// note: enum-type's comparation depend on types' list order of enum method
// so, if compared property is enum-type ,then its comparationfollow ObjEnum.objType order
return this.age.compareTo(otherAge);
}
}
主方法:
public static void main(String[] args)
{
List
StudentDTO s1 = new StudentDTO ();
s.setName("yuanyuan");
s.setAge(22);
studentList.add(s1);
StudentDTO s1 = new StudentDTO ();
s.setName("lily");
s.setAge(23);
studentList.add(s2);
Collections.sort(studentList); //按照age升序 22,23,
Collections.reverse(studentList); //按照age降序 23,22
}
第二种是比较器类实现Comparator接口,重写int compare(Object o1, Object o2)方法;
model类:
public class StudentDTO implements Comparable
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public ObjType getType()
{
return type;
}
public void setAge(int age)
{
this.age= age;
}
}
比较器类:
class MyCompartor implements Comparator
{
@Override
public int compare(Object o1, Object o2)
{
StudentDTO sdto1= (StudentDTO )o1;
StudentDTO sdto2= (StudentDTO )o2;
return sdto1.getAge.compareTo(stdo2.getAge())
}
}
主方法:
public static void main(String[] args)
{
List
StudentDTO s1 = new StudentDTO ();
s.setName("yuanyuan");
s.setAge(22);
studentList.add(s1);
StudentDTO s1 = new StudentDTO ();
s.setName("lily");
s.setAge(23);
studentList.add(s2);
MyComparetor mc = new MyComparetor();
Collections.sort(studentList,mc); //按照age升序 22,23,
Collections.reverse(studentList,mc); //按照age降序 23,22
}
附注:
1.对于数组的排序方法如下:
String[] names = {"王林", "杨宝", "李镇", "刘迪", "刘波"};
Arrays.sort(names, com.ibm.icu.text.Collator.getInstance(com.ibm.icu.util.ULocale.SIMPLIFIED_CHINESE));//升序;
System.out.println(Arrays.toString(names));
2.对于汉字的排序:可以尝试使用ICU4J会得到更好的结果,特别是姓为某些生僻字的时候,
用com.ibm.icu.text.Collator替换java.text.Collator,用com.ibm.icu.util.ULocale替换java.util.Locale
3.对于枚举类型的enum1.compareTo(enum2)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大,
而不是按照值的字母先后顺序比较的。
以上是java Collections.sort()实现List排序的默认方法和自定义方法的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Spring Boot简化了可靠,可扩展和生产就绪的Java应用的创建,从而彻底改变了Java开发。 它的“惯例惯例”方法(春季生态系统固有的惯例),最小化手动设置
