如何在JAVA8 lambda中对条件进行排序

时间:2017-03-03 09:03:52

标签: java lambda java-8 java-stream

我正在使用Java-8 lambda对列表进行排序。

这是

列表
[(t1, tester1), (t4, tester4), (t3, tester3), (t2, tester2)]
排序后

[(t2, tester2), (t1, tester1), (t3, tester3), (t4, tester4)]

我想得到上面的结果

String specialId = 't2'

List<Test> list = new ArrayList<>();

Test test1 = new Test();
test1.setId = "t1"
test1.setName = "tester1"
list.add(test1)

Test test2 = new Test();
test2.setId = "t4"
test2.setName = "tester4"
list.add(test2)

Test test3 = new Test();
test3.setId = "t3"
test3.setName = "tester3"
list.add(test3)

Test test4 = new Test();
test4.setId = "t2"
test4.setName = "tester2"
list.add(test4)

Stream<Test> s1 = list.stream.filter(t -> t.getId.equals(specialId));
Stream<Test> s2 = list.stream.filter(t -> !t.getId.equals(specialId))
                    .sorted(Comparator.comparing(Test::getName));

Stream<Test> s = Stream.concat(s1, s2);

List<Test> result = s.collect(Collectors.toList());

我得到了预期的结果。但我只想使用一次流。 我该如何排序?

3 个答案:

答案 0 :(得分:5)

您可以将特殊ID处理放入比较器中。可以使用thenComparing链接两个比较。

  1. 首先检查ID≠是否为特殊ID。自try: inp = raw_input("Enter Score: ") score = float(inp) if score > 1.0 or score < 0 raise except: print "Please enter a score number between 0.0 and 1.0" quit() if score >= 0.9 : print "A" elif score >= 0.8 : print "B" elif score >= 0.7 : print "C" elif score >= 0.6 : print "D" elif score < 0.6 : print "F" else: #your logic 起,这将在所有内容之前带来特殊ID。
  2. 然后按名称排序。
  3. false < true

答案 1 :(得分:2)

你可以进行一次排序。

public static void main(String[] args) {
    String specialId = "t2";

    List<Test> list = new ArrayList<>();

    Test test1 = new Test();
    test1.setId("t1");
    test1.setName("tester1");
    list.add(test1);

    Test test2 = new Test();
    test2.setId("t2");
    test2.setName("tester2");
    list.add(test2);

    Test test3 = new Test();
    test3.setId("t3");
    test3.setName("tester3");
    list.add(test3);

    Test test4 = new Test();
    test4.setId("t4");
    test4.setName("tester4");
    list.add(test4);

    List<Test> sorted = list
            .stream()
            .sorted((t1, t2) -> {
                if (t1.getId().equals(specialId)) {
                    return -1;
                }

                if (t2.getId().equals(specialId)) {
                    return 1;
                }

                return t1.getId().compareTo(t2.getId());
            })
            .collect(Collectors.toList());

    sorted.forEach(System.out::println);
}

static class Test {
    String id;
    String name;

    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;
    }

    @Override
    public String toString() {
        return "Test{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

答案 2 :(得分:1)

我认为你很清楚流被懒惰评估。

Stream<Test> s1 = list.stream.filter(t -> t.getId.equals(specialId));

您可以在另一个地方使用否定时取出Predicate

Predicate<Test> specialTestPred = t -> t.getId.equals(specialId);

Stream<Test> specialTestStream = list.stream.filter(specialTestPred);
Stream<Test> nonSpecialTestStream = list.stream.filter(specialTestPred.negate())
                    .sorted(Comparator.comparing(Test::getName));

List<Test> result = Stream.concat(specialTestStream , nonSpecialTestStream)
                          .collect(Collectors.toList());