Java排序ArrayList,带有按数字和字母顺序排列的自定义字段
我想按价格和名称对ArrayList进行排序。我在谷歌搜索了很长一段时间,但我无法解决它。它是否会在可序列化方面出现问题为了您的目的,您需要实现Java排序ArrayList,带有按数字和字母顺序排列的自定义字段,java,sorting,Java,Sorting,我想按价格和名称对ArrayList进行排序。我在谷歌搜索了很长一段时间,但我无法解决它。它是否会在可序列化方面出现问题为了您的目的,您需要实现可比较或比较器接口。及 通过阅读这些教程,您可以了解这两者之间的区别 考虑到您希望使用其价格对产品进行分类,然后使您的产品实现可比性,如下所示 public class Product implements Serializable{ private String id; private String name; private
可比较
或比较器
接口。及
通过阅读这些教程,您可以了解这两者之间的区别
考虑到您希望使用其价格对产品进行分类,然后使您的产品
实现可比性
,如下所示
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 + '}';
}
您需要另一个排序序列,现在是它的名称,然后:
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;
}
}
如果要使用名称进行排序,则
Collections.sort(yourList,new PriceSorter());
第二个参数采用
比较器
实例,它使排序方法知道在对对象进行排序时遵循什么逻辑您需要实现Comparable
接口。该接口要求您添加一个名为compareTo(Product other)
的函数,在该函数中,您可以编写代码来检查要比较对象的自定义字段
或者,您可以按照@Prasad Kharkar的建议编写一个Comaparator
,基本上完成相同的工作
据我所知,你没有这样的方法,你能做的是;扩展集合的子类并添加方法进行排序(搜索或冒泡排序等技术) 如果您有数据库设施(更多开销) *你可以把它放在那里,按顺序使用 *如果您使用的是JPA,只需将列表转储到实体类中使用
比较器
,这里是匿名实现的(适用于java 7和更早版本):
如果定义了产品的“自然顺序”,请考虑制作<代码>产品<代码>实现<代码>可比的<代码>,并在
类实现产品
接口可比
Collections.sort(list, Comparator.comparing(Product::getPrice).thenComparing(Product::getName));
以下是产品的完整源代码,它通过主方法中的排序示例实现了可比:
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);
}
}
import java.io.Serializable;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
公共类产品实现了可序列化、可比较的{
私有字符串id;
私有字符串名称;
私人双价;
私人整数数量;
公共产品(字符串id、字符串名称、双倍价格、整数数量){
this.id=id;
this.name=名称;
这个价格=价格;
这个。数量=数量;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公开双价{
退货价格;
}
公共定价(双倍价格){
这个价格=价格;
}
公共整数getQuantity(){
退货数量;
}
公共无效设置数量(整数数量){
这个。数量=数量;
}
@凌驾
公共字符串toString(){
返回“产品{”+“id=“+id+”,name=“+name+”,price=“+price
+,quantity=“+quantity+'}”;
}
@凌驾
公共内部比较(产品o){
int result=this.name.compareToIgnoreCase(o.name);
如果(结果!=0){
返回结果;
}否则{
返回新的双精度(此价格)。与(新的双精度(o价格))相比;
}
}
公共静态void main(字符串[]args){
产品p1=新产品(“p1”,“鞋”,30.33,20);
产品p2=新产品(“p2”,“鞋”,20.30,20);
产品p3=新产品(“p3”,“鞋”,50.33,20);
产品p4=新产品(“p4”,“袜子”,10.50,20);
产品p5=新产品(“p5”,“袜子”,5.40,20);
产品p6=新产品(“p6”,“袜子”,2.34,20);
列表产品=数组.asList(p1,p2,p3,p4,p5,p6);
System.out.println(“未排序”);
用于(产品:产品){
System.out.println(“产品:+Product.name+”价格:+Product.Price”);
}
收集、分类(产品);
系统输出打印项次(“排序”);
用于(产品:产品){
System.out.println(“产品:+Product.name+”价格:+Product.Price”);
}
}
}
先是价格,然后是名称?您是如何尝试对列表进行排序的?
?1箱带名称,1箱带价格。我不记得了。他们中的一些人在课堂上使用了集合,一些人实现了Comparable你是说Comparable,两者都可以解决这个问题Comparator
比Comparable
具有某些优势,在这种特定情况下,必须实现的是Comparable
。如果您的对象要在对象之间进行比较,则实现比较器
,而不是要在对象之间进行比较。@morgano,如果不允许用户更改产品
类的代码,则不可能实现。使用比较器
,他不必在产品
类中更改任何内容。好的,你可以使用匿名类。但如果我们想创建多个排序序列,那么必须使用Comparator。有一个问题,我可能不在这里,但原语double
没有compareTo
方法。由于getPrice
返回原语,是否发生了允许使用compareTo
的自动装箱?@haind您是否收到错误消息?还是不像你想象的那样?如果是这样,您希望排序如何运行?下面是结果:20.3 30.33 50.33 2.34 5.4 10。5@haind这就是我解释你想要的功能。它首先按产品名称排序,然后按价格排序,两者都按升序排列。我假设结果应该包含产品名称
Collections.sort(yourList, new NameSorter());
List<Product> list;
Collections.sort(list, new Comparator<Product>() {
public int compare(Product a, Product b) {
if (a.getPrice() == b.getPrice())
return a.getName().compareTo(b.getName());
return a.getPrice() > b.getPrice() ? 1 : a.getPrice() < b.getPrice() ? -1 : 0
}
});
Collections.sort(list, Comparator.comparing(Product::getPrice).thenComparing(Product::getName));
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));
}
}
}
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);
}
}
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);
}
}
}