<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<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> 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<String> 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>