<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          List 如何根據(jù)對(duì)象的屬性去重?Java 8 輕松搞定!

          共 2754字,需瀏覽 6分鐘

           ·

          2022-04-11 13:45

          一、去除List中重復(fù)的String

          public?List?removeStringListDupli(List?stringList)?{
          ????Set?set?=?new?LinkedHashSet<>();
          ????set.addAll(stringList);

          ????stringList.clear();

          ????stringList.addAll(set);
          ????return?stringList;
          }

          或使用Java8的寫法:

          List?unique?=?list.stream().distinct().collect(Collectors.toList());

          二、List中對(duì)象去重

          比如現(xiàn)在有一個(gè) Person類:

          public?class?Person?{
          ????private?Long?id;

          ????private?String?name;

          ????public?Person(Long?id,?String?name)?{
          ????????this.id?=?id;
          ????????this.name?=?name;
          ????}

          ????public?Long?getId()?{
          ????????return?id;
          ????}

          ????public?void?setId(Long?id)?{
          ????????this.id?=?id;
          ????}

          ????public?String?getName()?{
          ????????return?name;
          ????}

          ????public?void?setName(String?name)?{
          ????????this.name?=?name;
          ????}

          ????@Override
          ????public?String?toString()?{
          ????????return?"Person{"?+
          ????????????????"id="?+?id?+
          ????????????????",?name='"?+?name?+?'\''?+
          ????????????????'}';
          ????}
          }

          重寫Person對(duì)象的equals()方法和hashCode()方法:

          ?@Override
          public?boolean?equals(Object?o)?{
          ????if?(this?==?o)?return?true;
          ????if?(o?==?null?||?getClass()?!=?o.getClass())?return?false;

          ????Person?person?=?(Person)?o;

          ????if?(!id.equals(person.id))?return?false;
          ????return?name.equals(person.name);
          }

          @Override
          public?int?hashCode()?{
          ????int?result?=?id.hashCode();
          ????result?=?31?*?result?+?name.hashCode();
          ????return?result;
          }

          下面對(duì)象去重的代碼:

          Person?p1?=?new?Person(1l,?"jack");
          Person?p2?=?new?Person(3l,?"jack?chou");
          Person?p3?=?new?Person(2l,?"tom");
          Person?p4?=?new?Person(4l,?"hanson");
          Person?p5?=?new?Person(5l,?"膠布蟲");

          List?persons?=?Arrays.asList(p1,?p2,?p3,?p4,?p5,?p5,?p1,?p2,?p2);

          List?personList?=?new?ArrayList<>();
          //?去重
          persons.stream().forEach(
          ????p?->?{
          ????????if?(!personList.contains(p))?{
          ????????????personList.add(p);
          ????????}
          ????}
          );
          System.out.println(personList);

          List 的contains()方法底層實(shí)現(xiàn)使用對(duì)象的equals方法去比較的,其實(shí)重寫equals()就好,但重寫了equals最好將hashCode也重寫了。

          我們創(chuàng)建了一個(gè)高質(zhì)量的技術(shù)交流群,與優(yōu)秀的人在一起,自己也會(huì)優(yōu)秀起來,趕緊點(diǎn)擊加群,享受一起成長(zhǎng)的快樂。

          可以參見:

          • http://stackoverflow.com/questions/30745048/how-to-remove-duplicate-objects-from-java-arraylist
          • http://blog.csdn.net/growing_tree/article/details/46622579

          三、根據(jù)對(duì)象的屬性去重

          下面要根據(jù)Person對(duì)象的id去重,那該怎么做呢?

          寫一個(gè)方法吧:

          ??public?static?List?removeDupliById(List?persons)?{
          ????????Set?personSet?=?new?TreeSet<>((o1,?o2)?->?o1.getId().compareTo(o2.getId()));
          ????????personSet.addAll(persons);

          ????????return?new?ArrayList<>(personSet);
          ????}

          通過Comparator比較器,比較對(duì)象屬性,相同就返回0,達(dá)到過濾的目的。

          再來看比較炫酷的Java8寫法:

          import?static?java.util.Comparator.comparingLong;
          import?static?java.util.stream.Collectors.collectingAndThen;
          import?static?java.util.stream.Collectors.toCollection;

          //?根據(jù)id去重
          List?unique?=?persons.stream().collect(
          ????collectingAndThen(
          ????????toCollection(()?->?new?TreeSet<>(comparingLong(Person::getId))),?ArrayList::new)
          );

          這段炫酷的代碼是google的,還不明白是怎么個(gè)原理,等我好好研究一下,再專門寫篇文章好好闡述一下。

          還有一種寫法:

          public?static??Predicate?distinctByKey(Functionsuper?T,?Object>?keyExtractor)?{
          ????Map?map?=?new?ConcurrentHashMap<>();
          ????return?t?->?map.putIfAbsent(keyExtractor.apply(t),?Boolean.TRUE)?==?null;
          }

          //?remove?duplicate
          persons.stream().filter(distinctByKey(p?->?p.getId())).forEach(p?->?System.out.println(p));

          java8 確實(shí)簡(jiǎn)化了很多冗長(zhǎng)的操作,精簡(jiǎn)了代碼,小伙,研究java8去吧!

          轉(zhuǎn)自:Balalalalalalalala

          鏈接:https://blog.csdn.net/jiaobuchong/article/details/54412094

          推薦閱讀

          ··································

          你好,我是程序猿DD,10年開發(fā)老司機(jī)、阿里云MVP、騰訊云TVP、出過書、創(chuàng)過業(yè)、國(guó)企4年互聯(lián)網(wǎng)6年10年前畢業(yè)加入宇宙行,工資不高、也不算太忙,業(yè)余堅(jiān)持研究技術(shù)和做自己想做的東西。4年后離開國(guó)企,加入永輝互聯(lián)網(wǎng)板塊的創(chuàng)業(yè)團(tuán)隊(duì),從開發(fā)、到架構(gòu)、到合伙人。一路過來,給我最深的感受就是一定要不斷學(xué)習(xí)并關(guān)注前沿。只要你能堅(jiān)持下來,多思考、少抱怨、勤動(dòng)手,就很容易實(shí)現(xiàn)彎道超車!所以,不要問我現(xiàn)在干什么是否來得及。如果你看好一個(gè)事情,一定是堅(jiān)持了才能看到希望,而不是看到希望才去堅(jiān)持。相信我,只要堅(jiān)持下來,你一定比現(xiàn)在更好!如果你還沒什么方向,可以先關(guān)注我,這里會(huì)經(jīng)常分享一些前沿資訊,幫你積累彎道超車的資本。

          點(diǎn)擊閱讀原文,送你免費(fèi)Spring Boot教程
          瀏覽 41
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  中文字幕自拍 | 精品一区在线观看2025 | 美国一级操逼黄片 | 精品国产精品三级精品AV网址 | 久久久7777 |