這篇文章主要介紹了Java中List去重和Stream去重的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比正安網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式正安網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋正安地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
問題
當(dāng)下互聯(lián)網(wǎng)技術(shù)成熟,越來越多的趨向去中心化、分布式、流計(jì)算,使得很多以前在數(shù)據(jù)庫側(cè)做的事情放到了Java端。今天有人問道,如果數(shù)據(jù)庫字段沒有索引,那么應(yīng)該如何根據(jù)該字段去重?大家都一致認(rèn)為用Java來做,但怎么做呢?
解答
忽然想起以前寫過list去重的文章,找出來一看。做法就是將list中對(duì)象的hashcode和equals方法重寫,然后丟到HashSet里,然后取出來。這是最初剛學(xué)Java的時(shí)候像被字典一樣背寫出來的答案。就比如面試,面過號(hào)稱做了3年Java的人,問Set和HashMap的區(qū)別可以背出來,問如何實(shí)現(xiàn)就不知道了。也就是說,初學(xué)者只背特性。但真正在項(xiàng)目中使用的時(shí)候你需要確保一下是不是真的這樣。因?yàn)楸硶鴽]用,只能相信結(jié)果。你需要知道HashSet如何幫我做到去重了。換個(gè)思路,不用HashSet可以去重嗎?最簡單,最直接的辦法不就是每次都拿著和歷史數(shù)據(jù)比較,都不相同則插入隊(duì)尾。而HashSet只是加速了這個(gè)過程而已。
首先,給出我們要排序的對(duì)象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,為了防止扯皮,給個(gè)規(guī)則,只要任意取出id唯一的數(shù)據(jù)即可,不用拘泥id相同時(shí)算哪個(gè)。
用最直觀的辦法
這個(gè)辦法就是用一個(gè)空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可以去重,那么是如何去重的呢? 再深入一點(diǎn)的背過根據(jù)hashcode和equals方法。那么如何根據(jù)這兩個(gè)做到的呢?沒有看過源碼的人是無法繼續(xù)的,面試也就到此結(jié)束了。
事實(shí)上,HashSet是由HashMap來實(shí)現(xiàn)的(沒有看過源碼的時(shí)候曾經(jīng)一直直觀的以為HashMap的key是HashSet來實(shí)現(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實(shí)現(xiàn)的,而HashMap的實(shí)現(xiàn)又完全依賴于hashcode和equals方法。這下就徹底打通了,想用HashSet就必須看好自己的這兩個(gè)方法。
在本題目中,要根據(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。
最終實(shí)現(xiàn)如下:
@Test public void dis2() { Set<User> result = new HashSet<>(users); System.out.println(result); }
使用Java的Stream去重
回到最初的問題,之所以提這個(gè)問題是因?yàn)橄胍獙?shù)據(jù)庫側(cè)去重拿到Java端,那么數(shù)據(jù)量可能比較大,比如10w條。對(duì)于大數(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.
我們知道,也必須背過這樣一個(gè)準(zhǔn)則:equals返回true的時(shí)候,hashcode的返回值必須相同. 這個(gè)在背的時(shí)候略微有些邏輯混亂,但只要了解了HashMap的實(shí)現(xiàn)方式就不會(huì)覺得拗口了。HashMap先根據(jù)hashcode方法定位,再比較equals方法。
所以,要使用distinct來實(shí)現(xiàn)去重,必須重寫hashcode和equals方法,除非你使用默認(rèn)的。
那么,究竟為啥要這么做?點(diǎn)進(jìn)去看一眼實(shí)現(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實(shí)現(xiàn)的啊,想到reduce,瞬間想到一種自己實(shí)現(xiàn)distinctBykey的方法。我只要用reduce,計(jì)算部分就是把Stream的元素拿出來和我自己內(nèi)置的一個(gè)HashMap比較,有則跳過,沒有則放進(jìn)去。其實(shí),思路還是最開始的那個(gè)最直白的方法。
@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,則取出來的不一定是第一個(gè),而是隨機(jī)的。
上述方法是至今發(fā)現(xiàn)最好的,無侵入性的。但如果非要用distinct。只能像HashSet那個(gè)方法一樣重寫hashcode和equals。
小結(jié)
會(huì)不會(huì)用這些東西,你只能去自己練習(xí)過,不然到了真正要用的時(shí)候很難一下子就拿出來,不然就冒險(xiǎn)用。而若真的想大膽使用,了解規(guī)則和實(shí)現(xiàn)原理也是必須的。比如,LinkedHashSet和HashSet的實(shí)現(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); } }
補(bǔ)充:
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里的對(duì)象遍歷一遍,用list.contain(),如果不存在就放入到另外一個(gè)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; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java中List去重和Stream去重的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
網(wǎng)站名稱:Java中List去重和Stream去重的示例分析
當(dāng)前路徑:http://muchs.cn/article30/jchdso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、商城網(wǎng)站、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、定制開發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)