Sorting을 이용한 Java 8 lamda / (새로 추가된) javax.util.Comparator api 테스트 


java7은 comparator와 guava ordering을 통해서 sorting을 할 수 있다.


https://gist.github.com/knight1128/fbb711b1dd8eea38dd60

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 java8LamdaSortTest() {
        persons.sort((p1, p2) -> p1.age.compareTo(p2.age));
        System.out.println("java8LamdaSort : " + persons);
    }

    @Test
    public void java8ComparatorSortTest() {
        persons.sort(Comparator.comparing(Person::getAge));
        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);
    }
}





결과

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]]



* 참고

Comparator 클래스의 comparing 메소드의 내부는 Lamda로 구현되어 있다.

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor)
{
    Objects.requireNonNull(keyExtractor);
    return (Comparator<T> & Serializable)
        (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));

}


Posted by '김용환'
,