如何从列表基于自定义Java对象不是原始类型上删除重复?

编程入门 行业动态 更新时间:2024-10-10 07:30:35
本文介绍了如何从列表基于自定义Java对象不是原始类型上删除重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我发布这个问题,我发现了贴在某种程度上类似的问题here.但得到的答复是基于一个字符串。不过,我有一个不同的情况在这里。我不是试图消除字符串,但所谓的AwardYearSource另一个对象。这个类有一个名为一年一个int属性。所以,我想删除基于一年重复。即如果有2010年不止一次提到,我想删除AwardYearSource对象。我该怎么办呢?

Before I post this question, I found somehow similar question posted here. But the answer was based on a String. However, I have a different situation here. I am not trying to remove String but another object called AwardYearSource. This class has an int attribute called year. So I want to remove duplicates based on the year. i.e if there is year 2010 mentioned more than once, I want to remove that AwardYearSource object. How can I do that?

推荐答案

基于字段删除元素的最简单方法如下(preserving顺序排列):

The simplest way to remove elements based on a field is as follows (preserving order):

Map<Integer, AwardYearSource> map = new LinkedHashMap<>(); for (AwardYearSource ays : list) { map.put(ays.getYear(), ays); } list.clear(); list.addAll(map.values());

更多推荐

如何从列表基于自定义Java对象不是原始类型上删除重复?

本文发布于:2023-11-28 07:57:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   原始   对象   类型   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!