Skip to content

Instantly share code, notes, and snippets.

@knight1128
Created May 15, 2015
Embed
What would you like to do?
java 8 lamda sort vs java 7 sort
package com.google.locationlab;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.junit.Before;
import org.junit.Test;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Java8Test {
private static List<Person> persons;
@Before
public void before() {
persons = Lists.newArrayList();
persons.add(new Person(1, "Samuel", 30));
persons.add(new Person(2, "Jason", 25));
persons.add(new Person(3, "Brian", 55));
persons.add(new Person(4, "Cato", 32));
//System.out.println(persons);
}
// java.util.Collection sorting
@Test
public void java7CollectionSortTest() {
Collections.sort(persons, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.age.compareTo(p2.age);
}
});
System.out.println("java7CollectionSortTest : " + persons);
}
@Test
public void java7GuavaCollectionSortTest() {
Collections.sort(persons, new Ordering<Person>() {
@Override
public int compare(Person p1, Person p2) {
return Ints.compare(p1.age, p2.age);
}
});
System.out.println("java7GuavaCollectionSortTest : " + persons);
}
@Test
public void java8LamdaWithTypeCollectionSortTest() {
Collections.sort(persons, (Person p1, Person p2) -> Ints.compare(p1.age, p2.age));
System.out.println("java8LamdaWithTypeCollectionSortTest : " + persons);
}
@Test
public void java8LamdaWithoutTypeCollectionSortTest() {
Collections.sort(persons, (p1, p2) -> Ints.compare(p1.age, p2.age));
System.out.println("java8LamdaWithoutTypeCollectionSortTest : " + persons);
}
@Test
public void java8LamdaSort() {
persons.sort((p1, p2) -> p1.age.compareTo(p2.age));
System.out.println("java8LamdaSort : " + persons);
}
}
class Person {
Integer id;
String name;
Integer age;
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
@knight1128
Copy link
Author

knight1128 commented May 15, 2015

result

java8LamdaWithoutTypeCollectionSortTest : [Person[id=2,name=Jason,age=25], Person[id=1,name=Samuel,age=30], Person[id=4,name=Cato,age=32], Person[id=3,name=Brian,age=55]]
java8LamdaWithTypeCollectionSortTest : [Person[id=2,name=Jason,age=25], Person[id=1,name=Samuel,age=30], Person[id=4,name=Cato,age=32], Person[id=3,name=Brian,age=55]]
java7CollectionSortTest : [Person[id=2,name=Jason,age=25], Person[id=1,name=Samuel,age=30], Person[id=4,name=Cato,age=32], Person[id=3,name=Brian,age=55]]
java8LamdaSort : [Person[id=2,name=Jason,age=25], Person[id=1,name=Samuel,age=30], Person[id=4,name=Cato,age=32], Person[id=3,name=Brian,age=55]]
java7GuavaCollectionSortTest : [Person[id=2,name=Jason,age=25], Person[id=1,name=Samuel,age=30], Person[id=4,name=Cato,age=32], Person[id=3,name=Brian,age=55]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment