Java中對List去重 Stream去重的解決方法
問題
當(dāng)下互聯(lián)網(wǎng)技術(shù)成熟,越來越多的趨向去中心化、分布式、流計算,使得很多以前在數(shù)據(jù)庫側(cè)做的事情放到了Java端。今天有人問道,如果數(shù)據(jù)庫字段沒有索引,那么應(yīng)該如何根據(jù)該字段去重?大家都一致認(rèn)為用Java來做,但怎么做呢?
解答
忽然想起以前寫過list去重的文章,找出來一看。做法就是將list中對象的hashcode和equals方法重寫,然后丟到HashSet里,然后取出來。這是最初剛學(xué)Java的時候像被字典一樣背寫出來的答案。就比如面試,面過號稱做了3年Java的人,問Set和HashMap的區(qū)別可以背出來,問如何實現(xiàn)就不知道了。也就是說,初學(xué)者只背特性。但真正在項目中使用的時候你需要確保一下是不是真的這樣。因為背書沒用,只能相信結(jié)果。你需要知道HashSet如何幫我做到去重了。換個思路,不用HashSet可以去重嗎?最簡單,最直接的辦法不就是每次都拿著和歷史數(shù)據(jù)比較,都不相同則插入隊尾。而HashSet只是加速了這個過程而已。
首先,給出我們要排序的對象User
@Data
@Builder
@AllArgsConstructor
public class User {
private Integer id;
private String name;
}
List<User> users = Lists.newArrayList(
new User(1, "a"),
new User(1, "b"),
new User(2, "b"),
new User(1, "a"));
目標(biāo)是取出id不重復(fù)的user,為了防止扯皮,給個規(guī)則,只要任意取出id唯一的數(shù)據(jù)即可,不用拘泥id相同時算哪個。
用最直觀的辦法
這個辦法就是用一個空list存放遍歷后的數(shù)據(jù)。
@Test
public void dis1() {
List<User> result = new LinkedList<>();
for (User user : users) {
boolean b = result.stream().anyMatch(u -> u.getId().equals(user.getId()));
if (!b) {
result.add(user);
}
}
System.out.println(result);
}
用HashSet
背過特性的都知道HashSet可以去重,那么是如何去重的呢? 再深入一點的背過根據(jù)hashcode和equals方法。那么如何根據(jù)這兩個做到的呢?沒有看過源碼的人是無法繼續(xù)的,面試也就到此結(jié)束了。
事實上,HashSet是由HashMap來實現(xiàn)的(沒有看過源碼的時候曾經(jīng)一直直觀的以為HashMap的key是HashSet來實現(xiàn)的,恰恰相反)。這里不展開敘述,只要看HashSet的構(gòu)造方法和add方法就能理解了。
public HashSet() {
map = new HashMap<>();
}
/**
* 顯然,存在則返回false,不存在的返回true
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
那么,由此也可以看出HashSet的去重復(fù)就是根據(jù)HashMap實現(xiàn)的,而HashMap的實現(xiàn)又完全依賴于hashcode和equals方法。這下就徹底打通了,想用HashSet就必須看好自己的這兩個方法。
在本題目中,要根據(jù)id去重,那么,我們的比較依據(jù)就是id了。修改如下:
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return Objects.equals(id, user.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
//hashcode
result = 31 * result + (element == null ? 0 : element.hashCode());
其中, Objects調(diào)用Arrays的hashcode,內(nèi)容如上述所示。乘以31等于x<<5-x。
最終實現(xiàn)如下:
@Test
public void dis2() {
Set<User> result = new HashSet<>(users);
System.out.println(result);
}
使用Java的Stream去重
回到最初的問題,之所以提這個問題是因為想要將數(shù)據(jù)庫側(cè)去重拿到Java端,那么數(shù)據(jù)量可能比較大,比如10w條。對于大數(shù)據(jù),采用Stream相關(guān)函數(shù)是最簡單的了。正好Stream也提供了distinct函數(shù)。那么應(yīng)該怎么用呢?
users.parallelStream().distinct().forEach(System.out::println);
沒看到用lambda當(dāng)作參數(shù),也就是沒有提供自定義條件。幸好Javadoc標(biāo)注了去重標(biāo)準(zhǔn):
Returns a stream consisting of the distinct elements
(according to {@link Object#equals(Object)}) of this stream.
我們知道,也必須背過這樣一個準(zhǔn)則:equals返回true的時候,hashcode的返回值必須相同. 這個在背的時候略微有些邏輯混亂,但只要了解了HashMap的實現(xiàn)方式就不會覺得拗口了。HashMap先根據(jù)hashcode方法定位,再比較equals方法。
所以,要使用distinct來實現(xiàn)去重,必須重寫hashcode和equals方法,除非你使用默認(rèn)的。
那么,究竟為啥要這么做?點進去看一眼實現(xiàn)。
<P_IN> Node<T> reduce(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
// If the stream is SORTED then it should also be ORDERED so the following will also
// preserve the sort order
TerminalOp<T, LinkedHashSet<T>> reduceOp
= ReduceOps.<T, LinkedHashSet<T>>makeRef(LinkedHashSet::new, LinkedHashSet::add, LinkedHashSet::addAll);
return Nodes.node(reduceOp.evaluateParallel(helper, spliterator));
}
內(nèi)部是用reduce實現(xiàn)的啊,想到reduce,瞬間想到一種自己實現(xiàn)distinctBykey的方法。我只要用reduce,計算部分就是把Stream的元素拿出來和我自己內(nèi)置的一個HashMap比較,有則跳過,沒有則放進去。其實,思路還是最開始的那個最直白的方法。
@Test
public void dis3() {
users.parallelStream().filter(distinctByKey(User::getId))
.forEach(System.out::println);
}
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
當(dāng)然,如果是并行stream,則取出來的不一定是第一個,而是隨機的。
上述方法是至今發(fā)現(xiàn)最好的,無侵入性的。但如果非要用distinct。只能像HashSet那個方法一樣重寫hashcode和equals。
小結(jié)
會不會用這些東西,你只能去自己練習(xí)過,不然到了真正要用的時候很難一下子就拿出來,不然就冒險用。而若真的想大膽使用,了解規(guī)則和實現(xiàn)原理也是必須的。比如,LinkedHashSet和HashSet的實現(xiàn)有何不同。
附上賊簡單的LinkedHashSet源碼:
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
private static final long serialVersionUID = -2851667679971038690L;
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
}
public LinkedHashSet() {
super(16, .75f, true);
}
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true);
addAll(c);
}
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED);
}
}


補充:
Java中List集合去除重復(fù)數(shù)據(jù)的方法
1. 循環(huán)list中的所有元素然后刪除重復(fù)
public static List removeDuplicate(List list) {
for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
for ( int j = list.size() - 1 ; j > i; j -- ) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
return list;
}
2. 通過HashSet踢除重復(fù)元素
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
3. 刪除ArrayList中重復(fù)元素,保持順序
// 刪除ArrayList中重復(fù)元素,保持順序
public static void removeDuplicateWithOrder(List list) {
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
System.out.println( " remove duplicate " + list);
}
4.把list里的對象遍歷一遍,用list.contain(),如果不存在就放入到另外一個list集合中
public static List removeDuplicate(List list){
List listTemp = new ArrayList();
for(int i=0;i<list.size();i++){
if(!listTemp.contains(list.get(i))){
listTemp.add(list.get(i));
}
}
return listTemp;
}
相關(guān)文章
Java構(gòu)造函數(shù)里的一些坑記錄super()和this()
這篇文章主要介紹了Java構(gòu)造函數(shù)里的一些坑記錄super()和this(),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java集合之Comparable和Comparator接口詳解
Java提供了Comparable接口與Comparator接口,它們?yōu)閿?shù)組或集合中的元素提供了排序邏輯,實現(xiàn)此接口的對象數(shù)組或集合可以通過Arrays.sort或Collections.sort進行自動排序。本文將通過示例講講它們的使用,需要的可以參考一下2022-12-12
java中@EnableAutoConfiguration注解使用
在Spring Boot框架中,@EnableAutoConfiguration是一種非常重要的注解,本文就來介紹一下java中@EnableAutoConfiguration注解使用,感興趣的可以了解一下2023-11-11

