본문 바로가기
SpringBoot

java 8 remove duplicate object in List

by ByteBridge 2018. 9. 12.
반응형


자바 8으로 객체 리스트의 중복 데이터 제거하기



@Test
public void testaa() {
List<test> list = Arrays.asList(
new test("test1","email1","option1"),
new test("test1","email1","option2"),
new test("test1","email1","option3"),
new test("test1","email1","option3")
);

System.out.println(list.size());

List<test> list2 = new ArrayList<>();
//note: getOption 으로 비교
list2.addAll(list.stream().collect(Collectors.toConcurrentMap(test::getOption, Function.identity(), (p, q) -> p)).values());
for (test t:list2) {
System.out.println(t.option);
}
}
class test{
private String name;
private String email;
private String option;

public test(String name, String email, String option) {
this.name = name;
this.email = email;
this.option = option;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getOption() {
return option;
}

public void setOption(String option) {
this.option = option;
}
}


반응형