简体   繁体   English

对一个arraylist对象的值进行排序,并在一个lambda中打印它们的多个

[英]Sorting values of an arraylist objects and printing multiple of them in one lambda

My goal is to print out two values out of four - name (string) and hair colour (enum) - that describe a certain object in an arraylist. 我的目标是打印出四个中的两个值 - 名称(字符串)和头发颜色(枚举) - 描述arraylist中的某个对象。 The problem lies with sorting the printed list alphabetically by name. 问题在于按名称按字母顺序对打印列表进行排序。 It all has to be in one lambda. 这一切都必须在一个lambda。

I'm stuck with just printing it out, without sorting. 我只是打印出来而没有排序。 I've already set up the overrides but since I'm still learning, I can't seem to get it work. 我已经设置了覆盖但是因为我还在学习,我似乎无法让它工作。

public void printAllNamesColours() {
        people.stream()
            .forEach(n->System.out.println(n.getName() + " " + n.getColour()));
    }

@Override
public int compareTo(Person person) {
        int ret = name.compareToIgnoreCase(person.name);
        if (ret == 0) {
            ret = colour.compareTo(person.colour);
        }
        if (ret == 0) {
            ret = age - person.age;
        }
        return ret;
    }

You can sort it using Stream.sorted as: 您可以使用Stream.sorted对其进行排序:

people.stream()
      .sorted() // considering your `compareTo` implementation
      .forEach(n -> System.out.println(n.getName() + " " + n.getColour()));

As your Person objects are Comparable , you can directly call the sorted method and print the result. 由于您的Person对象是Comparable ,您可以直接调用sorted方法并打印结果。

people.stream()
      .sorted()
      .forEachOrdered(n->System.out.println(n.getName() + " " + n.getColour()));

note that I've used forEachOrdered as opposed to forEach to guarantee the elements are printed in the sorted order. 请注意,我使用forEachOrdered而不是forEach来保证元素以排序顺序打印。


You may also decide to sort my name only or by colour only without changing your current compareTo implementation, in which case you can use a custom comparator: 您可能还决定仅对我的名称进行排序或仅按颜色排序而不更改当前的compareTo实现,在这种情况下,您可以使用自定义比较器:

people.stream()
      .sorted(Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER))
      .forEachOrdered(n->System.out.println(n.getName() + " " + n.getColour()));

and by colour: 并按颜色:

people.stream()
      .sorted(Comparator.comparing(Person::getColour, String.CASE_INSENSITIVE_ORDER))
      .forEachOrdered(n->System.out.println(n.getName() + " " + n.getColour()));

As the other answer tells you, you need to add the .sorted() operation in the stream 正如另一个答案告诉您的那样,您需要在流中添加.sorted()操作


Next to that I'd like to suggest you a different way for comparing your Person : 接下来我想建议你一个不同的方式来比较你的Person

static Comparator<Person> comparator = 
        Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER)
                  .thenComparing(Person::getColour)
                  .thenComparing(Person::getAge);

@Override
public int compareTo(Person person) {
    return comparator.compare(this, person);
}

// with the different getters

listPersonVO.stream().sorted((o1, o2)->o1.getName().trim().compareTo(o2.getName().trim())).collect(Collectors.toList()); listPersonVO.stream()。sorted((o1,o2) - > o1.getName()。trim()。compareTo(o2.getName()。trim()))。collect(Collectors.toList());

enter image description here 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM