标签:string sort exce 包装类 精确 tco compareto cep 说明
Java实现对象排序的方式有两种:
自然排序:java.lang.Comparable
定制排序:java.util.Comparator
说明:
实现 Comparable接口 的类必须实现 compareTo(Object obj) 方法,两个对象即通过compareTo(Object obj) 的返回值来比较大小。
像String、包装类等实现了Comparable接口
实现 Comparable接口 的对象列表(和数组)可以通过 Collections.sort 或 Arrays.sort 进行自动排序。
最好使自然排序与 equals 一致
1、String类 已经实现了
import org.junit.Test;
import java.util.Arrays;
public class test01 {
@Test
public void testComparable(){
String[] arr = new String[]{"AA", "BB", "KK", "MM", "GG", "DD"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
2、重写 compareTo(Object obj) 方法
/**
* 假设我定义了一个 Goods类,我要指明商品比较大小的方式:
* 按照价格从低到高排序,再按照产品名称从高到低排序。
*/
public class Goods implements Comparable {
private String name;
private double price;
public Goods() {
}
public Goods(String name, double price) {
this.name = name;
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "Goods{" +
"name=‘" + name + ‘\‘‘ +
", price=" + price +
‘}‘;
}
//重写
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods = (Goods)o;
if(this.price > goods.price){
return 1;
}else if(this.price < goods.price){
return -1;
}else{
return this.name.compareTo(goods.name);
}
}
throw new RuntimeException("传入的数据类型不一致!");
}
}
import org.junit.Test;
import java.util.Arrays;
public class test01 {
@Test
public void testComparable(){
Goods[] arr = new Goods[2];
arr[0] = new Goods("feifei", 100);
arr[1] = new Goods("xiaoran", 50);
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
说明:
1、Goods类 举例
/**
* 假设我定义了一个 Goods类,这次有没有重写 compateTo 都行。
*/
public class Goods implements Comparable {
private String name;
private double price;
public Goods() {
}
public Goods(String name, double price) {
this.name = name;
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "Goods{" +
"name=‘" + name + ‘\‘‘ +
", price=" + price +
‘}‘;
}
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods = (Goods)o;
if(this.price > goods.price){
return 1;
}else if(this.price < goods.price){
return -1;
}else{
return this.name.compareTo(goods.name);
}
}
throw new RuntimeException("传入的数据类型不一致!");
}
}
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
public class test01 {
@Test
public void testComparable(){
Goods[] arr = new Goods[2];
arr[0] = new Goods("feifei", 100);
arr[1] = new Goods("xiaoran", 50);
//指明商品比较大小的方式:按照产品名称从低到高排序,再按照价格从高到低排序
Arrays.sort(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Goods && o2 instanceof Goods){
Goods g1 = (Goods) o1;
Goods g2 = (Goods) o2;
if(g1.getName().equals(g2.getName())){
return -Double.compare(g1.getPrice(), g2.getPrice());
}else{
return g1.getName().compareTo(g2.getName());
}
}
throw new RuntimeException("传入的数据类型不一致");
}
});
System.out.println(Arrays.toString(arr));
}
}
标签:string sort exce 包装类 精确 tco compareto cep 说明
原文地址:https://www.cnblogs.com/xiaoran991/p/12590512.html