【问题标题】:How Do You Write A Custom Java Comparator For A Specific String?如何为特定字符串编写自定义 Java 比较器?
【发布时间】:2020-11-27 08:32:09
【问题描述】:

您好,我想知道是否有人可以帮助我编写一些我坚持的代码。所以我正在使用 lambda 语句编写一个自定义比较器,我想执行以下操作。如果 .getName() 返回的名称彼此相等,那么我想从 .getDirection() 中选择字符串“Up”的名称(在这种情况下,它保证其中一个是“Up”,另一个是"Down"),否则我们将根据 .getType() 来查看哪个字母顺序更高。

到目前为止,我有这个: (x,y) -> x.getName().equals(y.getName()) ? //confused part : x.getType().compareTo(y.getType) );

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 如果 .getName() 返回的名称彼此相等,那么我想选择一个字符串“Up”(在这种情况下,它保证其中一个是“Up”) --> 什么是“向上”?
  • 让我编辑我搞砸的问题。我遗漏了一部分。
  • 你能澄清一下吗?如果两个对象的名称相同,你在比较哪些字段以及如何比较?
  • 所以我们在比较两个相同类型的对象。 .getName() 和 .getDirection() 返回字符串。因此,如果 x.getName() 的字符串与 y.getName() 的字符串相同,那么我将查看 x.getDirection() 或 y.getDirection() 是否是字符串“Up”。保证只有一个是“向上”。如果 x.getName() 和 y.getName() 不一样,那我就调用 x.getType() 和 y.getType(),按字母顺序挑选。
  • 好的,你检查一下 x.getDirection() 或 y.getDirection() 是否是字符串“Up”。假设 x.getDirection() 是“向上”。现在会发生什么?

标签: java lambda comparator


【解决方案1】:

不要试图对简洁过于贪婪。闭包很好,但目标不是编写最少数量的代码,而是只编写以易于理解的方式完成工作所需的代码(好吧,无论如何大部分时间......)

(x, y) -> {
    if (x.getName().equals(y.getName()) {
        return "Up".equals(x.getDirection()) ? -1 : 1;
    } else {
        return x.getType().compareTo(y.getType());
    }
}

【讨论】:

    【解决方案2】:

    我创建了一个示例类,希望对您有所帮助。

    package com;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    
    /**
     * @author jinleizhang
     */
    public class ComparatorTest {
      static class Car {
        String name;
        boolean up;
        String type;
    
        Car(String name, boolean up, String type) {
          this.name = name;
          this.up = up;
          this.type = type;
        }
    
        public String getName() {
          return this.name;
        }
    
        public boolean isUp() {
          return this.up;
        }
    
        public String getType() {
          return this.type;
        }
    
        @Override
        public String toString() {
          return "Car{" +
              "name='" + name + '\'' +
              ", up=" + up +
              ", type='" + type + '\'' +
              '}';
        }
      }
    
      public static void main(String[] args) {
        Car carA = new Car("NewCar", false, "type2020");
        Car carB = new Car("NewCar", true, "type2019");
        Car carC = new Car("OldCar", true, "type1990");
    
        List<Car> cars = new ArrayList<>();
        cars.add(carA);
        cars.add(carB);
        cars.add(carC);
        Collections.sort(cars, (x, y) -> {
          if (x.getName().equals(y.getName())) {
            return x.isUp() ? -1 : 1;
          }
    
          return x.getType().compareTo(y.getType());
        });
    
        for (var car : cars) {
          System.out.println(car);
        }
      }
    }
    
    

    输出

    Car{name='OldCar', up=true, type='type1990'}
    Car{name='NewCar', up=true, type='type2019'}
    Car{name='NewCar', up=false, type='type2020'}
    

    【讨论】:

      【解决方案3】:

      你不能把它写成比较器。

      假设你有这些对象:

      • A1:名称:A 方向:向下类型:1
      • A3:名称:A 方向:向上类型:3
      • B2:名称:B 方向:向上类型:2

      根据您的逻辑,A1 传递性。

      【讨论】:

      • 是的似乎是。知道为什么 OP 不会得到运行时异常说明违规吗? 1.8之后就去掉了吗?
      • 他们可能会在某个时候遇到异常。排序算法仅在碰巧检测到违规时才报告违规,它不会竭尽全力找到它们。
      猜你喜欢
      • 2021-01-16
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      相关资源
      最近更新 更多