Java8  Function과 Stream을 이용한 객체 복사 테스트 예제이다. 


List 값 비교는 Lamda를 사용하였다. 



import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


public class ConvertObjectTest {

Function<OneMember, OnePerson> convertFunction = new Function<OneMember, OnePerson>() {
public OnePerson apply(OneMember member) {
return new OnePerson(member.id, member.name);
}
};

@Test
public void convertObjectWithFunctionTest() {
OneMember member = new OneMember(1, "Samuel");
OnePerson person = convertFunction.apply(member);
Assert.assertEquals(member.name, person.name);
}

@Test
public void convertObjectListWithFunctionTest() {
OneMember samuel = new OneMember(1, "Samuel");
OneMember daisy = new OneMember(2, "Daisy");
OneMember matt = new OneMember(3, "Matt");
ImmutableList<OneMember> immutableMemberList = ImmutableList.of(samuel, daisy, matt);

List<OnePerson> personList = immutableMemberList.stream()
.map(convertFunction)
.collect(Collectors.<OnePerson>toList());

personList.sort((OnePerson h1, OnePerson h2) -> h1.id.compareTo(h2.id));
Assert.assertThat(personList.get(0), equalTo(new OnePerson(1, "Samuel")));
Assert.assertThat(personList.get(1), equalTo(new OnePerson(2, "Daisy")));
Assert.assertThat(personList.get(2), equalTo(new OnePerson(3, "Matt")));
}
}

class OneMember {
public Integer id;
public String name;
public OneMember(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

@Override
public boolean equals(Object obj) {
OneMember anotherMember = (OneMember) obj;
if (this.id == anotherMember.id && this.name.equals(anotherMember.name)) {
return true;
}
return false;
}
}

class OnePerson {
public Integer id;
public String name;

public OnePerson(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

@Override
public boolean equals(Object obj) {
OnePerson anotherPerson = (OnePerson) obj;
if (this.id == anotherPerson.id && this.name.equals(anotherPerson.name)) {
return true;
}
return false;
}
}



위의 코드 중 일부를 Java8 람다 스타일로 변경할 수 있다.


1) Function을 람다 표현식으로 변경

Function<OneMember, OnePerson> convertFunction = new Function<OneMember, OnePerson>() {
public OnePerson apply(OneMember member) {
return new OnePerson(member.id, member.name);
}
};

---> 


Function<OneMember, OnePerson> convertFunction = member -> new OnePerson(member.id, member.name);


2) Guava의 ImmutableList를 Streams.of로 변경

ImmutableList<OneMember> immutableMemberList = ImmutableList.of(samuel, daisy, matt);

List<OnePerson> personList = immutableMemberList.stream()
.map(convertFunction)
.collect(Collectors.<OnePerson>toList());

--->

List<OnePerson> persons = Stream.of(samuel, daisy, matt)
.map(convertFunction)
.sorted(comparing(onePerson -> onePerson.id))
.collect(toList());




Posted by '김용환'
,