Last active
April 20, 2018 16:59
-
-
Save sungitly/549edad48e40692d98250f976d99d7de to your computer and use it in GitHub Desktop.
Several approach to remove duplicates from list of beans in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.jetty.util.ConcurrentHashSet; | |
import java.util.*; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import io.reactivex.Observable; | |
import static java.util.Comparator.comparing; | |
import static java.util.stream.Collectors.toCollection; | |
import static org.hamcrest.Matchers.hasSize; | |
import static org.junit.Assert.assertThat; | |
/** | |
* @author Lei Sun <[email protected]> | |
*/ | |
public class DuplicatesRemoval { | |
public static void main(String[] args) { | |
List<UserBean> users = Arrays.asList( | |
new UserBean("u1", "Lei"), | |
new UserBean("u2", "Meimei"), | |
new UserBean("u1", "Allen"), | |
new UserBean("u3", "Tom"), | |
new UserBean("u2", "Alice")); | |
DuplicatesRemoval test = new DuplicatesRemoval(); | |
Collection<UserBean> resultOldFashion = test.removeDuplicatesOldFashion(users); | |
System.out.println(resultOldFashion); | |
assertThat(resultOldFashion, hasSize(3)); | |
Collection<UserBean> resultFancy = test.removeDuplicatesFancyStream(users); | |
System.out.println(resultFancy); | |
assertThat(resultFancy, hasSize(3)); | |
Collection<UserBean> resultFancyAlt = test.removeDuplicatesFancyStreamAlt(users); | |
System.out.println(resultFancyAlt); | |
assertThat(resultFancyAlt, hasSize(3)); | |
Collection<UserBean> resultFancyAlt2 = test.removeDuplicatesFancyStreamAlt2(users); | |
System.out.println(resultFancyAlt2); | |
assertThat(resultFancyAlt2, hasSize(3)); | |
Collection<UserBean> resultRxAha = test.removeDuplicatesRxAha(users); | |
System.out.println(resultRxAha); | |
assertThat(resultRxAha, hasSize(3)); | |
} | |
private Collection<UserBean> removeDuplicatesOldFashion(List<UserBean> users) { | |
Map<String, UserBean> map = new HashMap<>(); | |
users.forEach(u -> map.put(u.getCode(), u)); | |
return map.values(); | |
} | |
private Collection<UserBean> removeDuplicatesFancyStream(List<UserBean> users) { | |
return users.stream().collect(Collectors.toMap(UserBean::getCode, Function.identity(), (p, q) -> p)).values(); | |
} | |
private Collection<UserBean> removeDuplicatesFancyStreamAlt(List<UserBean> users) { | |
return users.stream().collect(toCollection(() -> new TreeSet<>(comparing(UserBean::getCode)))); | |
} | |
private Collection<UserBean> removeDuplicatesFancyStreamAlt2(List<UserBean> users) { | |
Set<String> seen = new HashSet<>(); | |
return users.stream().filter(u -> seen.add(u.getCode())).collect(Collectors.toList()); | |
} | |
private Collection<UserBean> removeDuplicatesRxAha(List<UserBean> users) { | |
return Observable.fromIterable(users).distinct(UserBean::getCode).toList().blockingGet(); | |
} | |
static class UserBean { | |
private String code; | |
private String nickname; | |
public UserBean(String code, String nickname) { | |
this.code = code; | |
this.nickname = nickname; | |
} | |
public String getCode() { | |
return code; | |
} | |
public void setCode(String code) { | |
this.code = code; | |
} | |
public String getNickname() { | |
return nickname; | |
} | |
public void setNickname(String nickname) { | |
this.nickname = nickname; | |
} | |
@Override | |
public String toString() { | |
return "UserBean{" + | |
"code='" + code + '\'' + | |
", nickname='" + nickname + '\'' + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment