Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Created April 21, 2014 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhewedy/11139654 to your computer and use it in GitHub Desktop.
Save mhewedy/11139654 to your computer and use it in GitHub Desktop.
java8 parallel sort
package helloJava8;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class LambdaExpr {
public static void main(String[] args) throws IOException {
List<Employee> list = new ArrayList<>();
list.add(new Employee("Muhammad", "Abdullah"));
list.add(new Employee("Ahmad", "Yaser"));
list.add(new Employee("Ahmad", "Abdullah"));
list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
.forEach(System.out::println);
System.out.println("--------------");
list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
.forEach(System.out::println);
}
static class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment