7

I am using the stream API in java 8 to handle my collections. However, I am wondering what would be an elegant way to sort my objects in a given order, using this API.

SortedCollection = inputCollection.stream()
    .map(e -> {
        return new Element(e.id, e.color);
    }).collect(Collectors.toList());

Here I would like to sort the elements that have been mapped, using the id attribute.

Any idea ? Thanks !

2
  • You could use the .sort(List) method, using a custom Comparable<T> interface Commented Feb 2, 2018 at 15:29
  • 1
    Didn't your IDE sugest .sort() on the output of map()? Commented Feb 2, 2018 at 15:29

3 Answers 3

16

Simply use Stream#sorted as follows with the name of your getter method for the Element's ID.

inputCollection.stream()
               .map(e -> new Element(e.id, e.color))
               .sorted(Comparator.comparing(Element::getId))
               .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that's exactly what I was looking for !
1

Maybe try this

SortedCollection = inputCollection.stream()
                .map(e -> {
                    return new Element(e.id, e.color);
                })
                .collect(Collectors.collectingAndThen(Collectors.toList(), l -> l.sort(yourComparator)));

Comments

1

Full detail example: May be helpful for the starter on Stream class.

  public class Employee {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        int salary;

        public Employee(String n, int s) {
            this.name = n;
            this.salary = s;
        }

        @Override
        public String toString() {
            return "[" + name + ", " + salary + "]";
        }

        public static void main(String[] args) {
            List<Employee> list = new ArrayList<Employee>() {
                {
                    add(new Employee("Joe", 50000));
                    add(new Employee("Jim", 75000));
                    add(new Employee("Tom", 80000));
                    add(new Employee("Jim", 70000));
                    add(new Employee("Steve", 55000));
                    add(new Employee("Jim", 100000));
                    add(new Employee("Joe", 59000));
                    add(new Employee("Rich", 88000));
                }
            };
            List<Employee> listSorted = new ArrayList<Employee>();
            Comparator<Employee>  NameCommparator = Comparator.comparing(Employee::getName);
            listSorted = list.stream().sorted(NameCommparator).collect(Collectors.toList());

            System.out.println(listSorted);

//sorting on descending order
            Comparator<Employee>  SalaryCommparator = Comparator.comparing(Employee::getSalary);
            listSorted = list.stream().sorted(SalaryCommparator.reversed()).collect(Collectors.toList());
            System.out.println(listSorted);


        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.