public class Product implements Serializable{
private String id;
private String name;
private double price ;
private int quantity;
public Product(String id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Product{" + "id=" + id + ", name=" + name + ", price=" + price + ", quantity=" + quantity + '}';
}
Run Code Online (Sandbox Code Playgroud)
我想ArrayList<Product>按价格和名字排序.我搜索谷歌很长一段时间但我无法解决它.它是否可以与Serializable有问题
Pra*_*kar 28
您需要为您的目的实现Comparable或Comparator接口.使用Comparator 对用户定义的对象进行排序,并对用户定义的对象进行排序
您可以通过阅读这些教程来了解这两者之间的区别
考虑您希望使用其价格对产品进行分类,然后按照以下方式制作Product工具Comparable
public class Product implements Comparable<Product>{
public int compareTo(Product other){
// your logic here
}
}
Run Code Online (Sandbox Code Playgroud)
但请坚持......现在我们已经实现Comparable了使用其价格对对象进行排序的界面,我们如何使用其他排序顺序对它们进行排序?我们只有一个compareTo()方法,我们不能在同一个类中编写单独的排序顺序.这是角色Comparator.使用Comparator,您可以定义多个排序顺序.
假设我们想要使用其价格进行排序,那么:
public class PriceSorter implements Comparator<Product>{
public int compare(Product one, Product another){
int returnVal = 0;
if(one.getPrice() < another.getPrice()){
returnVal = -1;
}else if(one.getPrice() > another.getPrice()){
returnVal = 1;
}else if(one.getPrice() == another.getPrice()){
returnVal = 0;
}
return returnVal;
}
}
Run Code Online (Sandbox Code Playgroud)
你想要另一个排序顺序,现在是它的名字,然后:
public class NameSorter implements Comparator<Product>{
public int compare(Product one, Product another){
return one.getName().compareTo(another.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当你想用价格排序时,那么
Collections.sort(yourList,new PriceSorter());
Run Code Online (Sandbox Code Playgroud)
如果要使用名称进行排序,那么
Collections.sort(yourList, new NameSorter());
Run Code Online (Sandbox Code Playgroud)
第二个参数采用Comparator实例,该实例使sort方法知道在排序对象时要遵循的逻辑
让Product类实现Comparable接口.
public class Product implements Serializable, Comparable<Product> {
//Ommitted constructors, fields and accessors
//This is an ascending sort order
@Override
public int compareTo(Product o) {
int result = this.name.compareToIgnoreCase(o.name);
if(result != 0){
return result;
}else{
return new Double(this.price).compareTo(new Double(o.price));
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后排序就像传递List给Collections.sort():
public static void main(String[] args) {
Product p1 = new Product("p1", "shoes", 30.33, 20);
Product p2 = new Product("p2", "shoes", 20.30, 20);
Product p3 = new Product("p3", "shoes", 50.33, 20);
Product p4 = new Product("p4", "socks", 10.50, 20);
Product p5 = new Product("p5", "socks", 5.40, 20);
Product p6 = new Product("p6", "socks", 2.34, 20);
List<Product> products = Arrays.asList(p1,p2,p3,p4,p5,p6);
System.out.println("Unsorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
Collections.sort(products);
System.out.println("sorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是该Product实现的完整源代码,其中Comparable包含方法中的排序示例main:
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Product implements Serializable, Comparable<Product> {
private String id;
private String name;
private double price;
private int quantity;
public Product(String id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Product{" + "id=" + id + ", name=" + name + ", price=" + price
+ ", quantity=" + quantity + '}';
}
@Override
public int compareTo(Product o) {
int result = this.name.compareToIgnoreCase(o.name);
if(result != 0){
return result;
}else{
return new Double(this.price).compareTo(new Double(o.price));
}
}
public static void main(String[] args) {
Product p1 = new Product("p1", "shoes", 30.33, 20);
Product p2 = new Product("p2", "shoes", 20.30, 20);
Product p3 = new Product("p3", "shoes", 50.33, 20);
Product p4 = new Product("p4", "socks", 10.50, 20);
Product p5 = new Product("p5", "socks", 5.40, 20);
Product p6 = new Product("p6", "socks", 2.34, 20);
List<Product> products = Arrays.asList(p1,p2,p3,p4,p5,p6);
System.out.println("Unsorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
Collections.sort(products);
System.out.println("sorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
}
}
Run Code Online (Sandbox Code Playgroud)