java8 list中根據對象多屬性去重

<div id="content_views" class="markdown_views prism-atom-one-dark"> <!-- flowchart 箭頭圖標 勿刪 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path> </svg> <p><font font-weight="bold" size="5px" face="楷體">強烈推薦一個大神的人工智能的教程:<a href="http://www.captainbed.net/zhanghan" rel="nofollow" data-token="7867e4ab53b1229e2c87aaa4eb7e9f26">http://www.captainbed.net/zhanghan</a><br> 原文地址:https://blog.csdn.net/zh15732621679/article/details/80483617 </font></p> <h1 id="背景"><a name="t0"></a><div class="table-box"><table><tbody><tr><td height="40px" bgcolor="#blur"><font size="6px" face="華文新魏" color="white">項目需求描述</font></td></tr></tbody></table></div></h1> <font font-weight="bold" size="4px" face="楷體">出於項目須要,有一個list,實體包含多個字段,當其中兩個字段相同均相同時,就認爲list中的兩條記錄是同樣的,原本想經過分組實現,java8的分組只提供根據一個字段分組,只好另尋他路,java8有一個collectingAndThen能夠根據多個字段去重,由於咱們的需求是能夠去重以後操做,所以採用這種方式。</font> <h1 id="背景"><a name="t1"></a><div class="table-box"><table><tbody><tr><td height="40px" bgcolor="#blur"><font size="6px" face="華文新魏" color="white">分組及去重</font></td></tr></tbody></table></div></h1> <font font-weight="bold" size="4px" face="楷體"> </font><ul> <li><strong>分組</strong></li> </ul> <pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">classEntities.stream().collect(Collectors.groupingBy(ClassEntity::getGrade)); <div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre> <ul> <li><strong>java8去重(根據年級和專業,當年級和專業都相同的狀況下看作是重複數據)</strong></li> </ul> <pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">List&lt;ClassEntity&gt; distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -&gt; new TreeSet&lt;&gt;(Comparator.comparing(o -&gt; o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new)); <div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre> <ul> <li><strong>經過hashSet去重(如將classNames去重):該種去重是bean徹底相同的時候算重複數據</strong></li> </ul> <pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">List&lt;String&gt; classNameList = new ArrayList(new HashSet(classNames));java

<div class="hljs-button {2}" data-title="複製"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>web

</div>
相關文章
相關標籤/搜索