文章目录
Comparable接口
观察ClassCastException
import java.util.Arrays;
class Book {
private String title ;
private double price ;
public Book(String title, double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return "【Book】图书名称:" + this.title + "、图书价格:" + this.price + "\n" ;
}
// setter、getter、无参构造,略 ...
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
Book books [] = new Book[] {
new Book("Java从入门到项目实战", 99.8) ,
new Book("Python从入门到项目实战", 89.7) ,
new Book("GO语言从入门到项目实战", 96.3)
} ; // 提供有一个对象数组
Arrays.sort(books); // 数组排序处理
System.out.println(Arrays.toString(books)); // 实现对象数组的字符串转换
}
}
实现对象数组的排序
package com.yootk.demo;
import java.util.Arrays;
class Book implements Comparable<Book> { // 这个类拥有排序支持
private String title ;
private double price ;
public Book(String title, double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return "【Book】图书名称:" + this.title + "、图书价格:" + this.price + "\n" ;
}
@Override
public int compareTo(Book o) {
if (this.price > o.price) {
return 1 ;
} else if (this.price < o.price) {
return -1 ;
} else {
return 0 ;
}
}
// setter、getter、无参构造,略 ...
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
Book books [] = new Book[] {
new Book("Java从入门到项目实战", 99.8) ,
new Book("Python从入门到项目实战", 89.7) ,
new Book("GO语言从入门到项目实战", 96.3)
} ; // 提供有一个对象数组
Arrays.sort(books); // 数组排序处理
System.out.println(Arrays.toString(books)); // 实现对象数组的字符串转换
}
}
Comparator接口
使用 Comparator使用对象数组排序
package com.yootk.demo;
import java.util.Arrays;
import java.util.Comparator;
class Book{ // 这个类拥有排序支持
private String title ;
private double price ;
public Book(String title, double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return "【Book】图书名称:" + this.title + "、图书价格:" + this.price + "\n" ;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
}
class BookComparator implements Comparator<Book> { // 提供有专属的比较器实现类
@Override
public int compare(Book o1, Book o2) {
if (o1.getPrice() > o2.getPrice()) {
return 1 ;
} else if (o1.getPrice() < o2.getPrice()) {
return -1 ;
} else {
return 0;
}
}
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
Book books [] = new Book[] {
new Book("Java从入门到项目实战", 99.8) ,
new Book("Python从入门到项目实战", 89.7) ,
new Book("GO语言从入门到项目实战", 96.3)
} ; // 提供有一个对象数组
Arrays.sort(books, new BookComparator()); // 数组排序处理
System.out.println(Arrays.toString(books)); // 实现对象数组的字符串转换
}
}
通过Lambda表达式实现简写
import java.util.Arrays;
import java.util.Comparator;
class Book{ // 这个类拥有排序支持
private String title ;
private double price ;
public Book(String title, double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return "【Book】图书名称:" + this.title + "、图书价格:" + this.price + "\n" ;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
Book books [] = new Book[] {
new Book("Java从入门到项目实战", 99.8) ,
new Book("Python从入门到项目实战", 89.7) ,
new Book("GO语言从入门到项目实战", 96.3)
} ; // 提供有一个对象数组
Comparator<Book> comparator = (o1, o2)->{
if (o1.getPrice() > o2.getPrice()) {
return 1 ;
} else if (o1.getPrice() < o2.getPrice()) {
return -1 ;
} else {
return 0;
}
} ;
Arrays.sort(books, comparator.reversed()); // 数组排序处理
System.out.println(Arrays.toString(books)); // 实现对象数组的字符串转换
}
}