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 !

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48
E. Bavoux
  • 535
  • 1
  • 4
  • 11

3 Answers3

15

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());
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
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)));
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);


        }
    }
Amir
  • 1,855
  • 3
  • 24
  • 40