-
java 8 remove duplicate object in ListTech/SpringBoot 2018. 9. 12. 11:55반응형
자바 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;
}
}반응형'Tech > SpringBoot' 카테고리의 다른 글
spring boot exception 처리를 위한 enum 클래스 활용 (0) 2018.09.15 Spring boot jar 파일 Linux 서버 실행 등록 하기 (0) 2018.09.12 Send Global sms with AWS SMS (0) 2018.08.23 Spring Security with CustomFilter (0) 2018.08.05 Java 8 - Functional interface 사용방법 (0) 2018.07.25