有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java按X排序,但按Y打印

在我的java程序中,有一个名为salesperson的类的对象

这些对象有一个名为sales的属性和一个名为name的属性

我想将销售价值进行比较,找出最大的价值,然后打印出具有最高销售价值的对象的名称

我是怎么做到的

double highSales;
String highSalesString;
highSales=first.getJanSales();
        highSalesString=first.getSName() +" "+ first.getTitle();
        if (second.getJanSales()>highSales)
        {
            highSales=second.getJanSales();
            highSalesString=second.getSName() +" "+ second.getTitle();
        }
        if (third.getJanSales()>highSales)
        {
            highSales=third.getJanSales();
            highSalesString=third.getSName() +" "+ third.getTitle();
        }

我觉得有一个更简单的方法来实现这一点


共 (2) 个答案

  1. # 1 楼答案

    Comparator

    您只需要使用^{}或相应地实现^{}

    比如说

    List<SalesPerson> persons = ...
    
    persons.sort(Comparator.comparingInt(SalesPerson::getSales).reverseOrder());
    
    SalesPerson personWithMostSales = persons.get(0);
    System.out.println(personWithMostSales.getName());
    

    如果您只需要这一个条目,则不必对整个列表进行排序:

    SalesPerson personWithMostSales = persons.stream()
        .max(Comparator.comparingInt(SalesPerson::getSales).reverseOrder())
        .orElseThrow();
    System.out.println(personWithMostSales.getName());
    

    还有^{}可以用来代替流


    Comparable

    如果您认为默认情况下按salesSalesPerson进行排序是合理的(所谓的自然顺序),您也可以让他们相应地实现^{}

    public class SalesPerson implements Comparable<? super SalesPerson> {
        ...
    
        @Override
        public int compareTo(SalesPerson other) {
            return -1 * Integer.compare(sales, other.sales);
        }
    }
    

    然后您可以将它们放入一个排序的集合中,例如TreeSet,这样它就可以为您保持它们的排序了。但是请注意,此类集合通常不允许在人员已添加到集合中时修改sales

  2. # 2 楼答案

    I want to compare the sales values to find the largest value and then print out the name of the object that has the highest sale value.

    我会跳过排序,因为你想要的似乎是拥有最大销售额的人的名字。假设您的销售人员在列表中,您可以按如下方式进行:

    用于演示目的的记录(行为类似于类)

    record SalesPerson(String getName, double getSales) {
        @Override
        public String toString() {
            return "Name: " + getName + ", " + "Sales: $" + getSales;
        }
    }
    

    创建一些数据

    List<SalesPerson> sales = List.of(new SalesPerson("John", 492.44 ),
            new SalesPerson("Mary", 688.68), new SalesPerson("Bob", 533.22));
    
    
    • 这将流式处理销售人员实例
    • 然后使用比较器,根据销售价值找到最大值
      max返回一个Optional<SalesPerson>
    Optional<SalesPerson> bestSalesPerson = sales.stream()
            .max(Comparator.comparingDouble(SalesPerson::getSales));
    
    if (bestSalesPerson.isPresent()) {
        System.out.println(bestSalesPerson.get());
    }
    

    印刷品

    Name: Mary, Sales: $688.68
    
    

    如果你不想使用流,你可以通过循环来实现

    • 如果列表不是空的,则将第一个人分配给best
    • 然后迭代列表,将每个候选人的记录与当前最佳记录进行比较,并根据需要进行调整。如果列表为空,则会添加额外的检查。如果没有必要,可以消除这些问题
    SalesPerson best = null;
    if (!sales.isEmpty()) {
        best = sales.get(0);
    }
    for (int i = 1; i < sales.size(); i++) {
        SalesPerson candidate = sales.get(i);
        if (candidate.getSales > best.getSales) {
            best = candidate;
        }
    }
    
    if (best != null) {
        System.out.println(best);
    }