Java中對List去重, Stream去重
每天學習一點點 編程PDF電子書、視頻教程免費下載:
http://www.shitanlife.com/code
問題
當下互聯網技術成熟,越來越多的趨向去中心化、分布式、流計算,使得很多以前在數據庫側做的事情放到了Java端。今天有人問道,如果數據庫字段沒有索引,那麽應該如何根據該字段去重?大家都一致認為用Java來做,但怎麽做呢?
解答
忽然想起以前寫過list去重的文章,找出來一看。做法就是將list中對象的hashcode和equals方法重寫,然後丟到HashSet裏,然後取出來。這是最初剛學Java的時候像被字典一樣背寫出來的答案。就比如面試,面過號稱做了3年Java的人,問Set和HashMap的區別可以背出來,問如何實現就不知道了。也就是說,初學者只背特性。但真正在項目中使用的時候你需要確保一下是不是真的這樣。因為背書沒用,只能相信結果。你需要知道HashSet如何幫我做到去重了。換個思路,不用HashSet可以去重嗎?最簡單,最直接的辦法不就是每次都拿著和歷史數據比較,都不相同則插入隊尾。而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"));
目標是取出id不重復的user,為了防止扯皮,給個規則,只要任意取出id唯一的數據即可,不用拘泥id相同時算哪個。
用最直觀的辦法
這個辦法就是用一個空list存放遍歷後的數據。
@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可以去重,那麽是如何去重的呢? 再深入一點的背過根據hashcode和equals方法。那麽如何根據這兩個做到的呢?沒有看過源碼的人是無法繼續的,面試也就到此結束了。
事實上,HashSet是由HashMap來實現的(沒有看過源碼的時候曾經一直直觀的以為HashMap的key是HashSet來實現的,恰恰相反)。這裏不展開敘述,只要看HashSet的構造方法和add方法就能理解了。
public HashSet() {
map = new HashMap<>();
}
/**
* 顯然,存在則返回false,不存在的返回true
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
那麽,由此也可以看出HashSet的去重復就是根據HashMap實現的,而HashMap的實現又完全依賴於hashcode和equals方法。這下就徹底打通了,想用HashSet就必須看好自己的這兩個方法。
在本題目中,要根據id去重,那麽,我們的比較依據就是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調用Arrays的hashcode,內容如上述所示。乘以31等於x<<5-x。
最終實現如下:
@Test
public void dis2() {
Set<User> result = new HashSet<>(users);
System.out.println(result);
}
使用Java的Stream去重
回到最初的問題,之所以提這個問題是因為想要將數據庫側去重拿到Java端,那麽數據量可能比較大,比如10w條。對於大數據,采用Stream相關函數是最簡單的了。正好Stream也提供了distinct函數。那麽應該怎麽用呢?
users.parallelStream().distinct().forEach(System.out::println);
沒看到用lambda當作參數,也就是沒有提供自定義條件。幸好Javadoc標註了去重標準:
Returns a stream consisting of the distinct elements
(according to {@link Object#equals(Object)}) of this stream.
我們知道,也必須背過這樣一個準則:equals返回true的時候,hashcode的返回值必須相同. 這個在背的時候略微有些邏輯混亂,但只要了解了HashMap的實現方式就不會覺得拗口了。HashMap先根據hashcode方法定位,再比較equals方法。
所以,要使用distinct來實現去重,必須重寫hashcode和equals方法,除非你使用默認的。
那麽,究竟為啥要這麽做?點進去看一眼實現。
<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));
}
內部是用reduce實現的啊,想到reduce,瞬間想到一種自己實現distinctBykey的方法。我只要用reduce,計算部分就是把Stream的元素拿出來和我自己內置的一個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));
}
當然,如果是並行stream,則取出來的不一定是第一個,而是隨機的。
上述方法是至今發現最好的,無侵入性的。但如果非要用distinct。只能像HashSet那個方法一樣重寫hashcode和equals。
小結
會不會用這些東西,你只能去自己練習過,不然到了真正要用的時候很難一下子就拿出來,不然就冒險用。而若真的想大膽使用,了解規則和實現原理也是必須的。比如,LinkedHashSet和HashSet的實現有何不同。
附上賊簡單的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);
}
}
每天學習一點點 編程PDF電子書、視頻教程免費下載:
http://www.shitanlife.com/code
Java中對List去重, Stream去重