将列表传递给集合构造函数,过滤是否按顺序发生?
我正在从 JPA 查询中检索一个列表,按 effectiveDate 排序。除了日期列之外,可能会有重复的条目,我将首先对它们进行最近的日期排序(描述)。我想要的只是集合中具有最新 effectiveDate 的所有条目;该集合中不允许使用较旧有效日期的重复内容。
如果我通过将此列表传递到构造函数来创建 HashSet,则新集是否仅包含列表中的第一个条目,仅包含具有“最新”有效日期的条目?
换句话说,当从列表创建集合时,集合是否按列表顺序初始化?
谢谢!
I am retrieving a List from a JPA query, ordered by effectiveDate. There could be duplicate entries except for the date column, and I'll be ordering them most recent date first (desc). All I want in the set are all the entries with the newest effectiveDate; dupes with older effectiveDates are not allowed in the Set.
If I create a HashSet by passing this List into the constructor, does the new Set only contain the first entries in the List, only the ones with the "newest" effectiveDates?
In other words, are Sets initialized in List order when they are created from a List?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您从包含重复项的集合创建
HashSet
,则只有每个重复项中的第一个才会添加到哈希集中。它 按顺序添加列表中的项目,add方法不会覆盖重复项。
尽管未指定此行为,但它不太可能改变。
If you create a
HashSet
from a collection with duplicates, only the first of each duplicate will be added to the hashset.It adds the items in the list in order, and the add method does not overwrite duplicates.
Although this behavior is not specified, it's highly unlikely to change.
通过“重复条目”,您的意思是查询返回了不同的行/对象实例,并且您已经以不包含此 effectiveDate 字段的方式定义了 equals/hashCode 吗?换句话说,如果你说 list.get(i) 和 list.get(j) 除日期之外都是重复的,那么你的意思是 list.get(i) != list.get(j) 和 list.get( i).equals(list.get(j)).
如果这就是你的意思,那么我相信第一个插入到集合中的人获胜。
By "duplicate entries", do you mean that there are distinct rows/object instances returned by your query, and that you have defined equals/hashCode in a way such that they do not include this effectiveDate field? In other words, if you say that list.get(i) and list.get(j) are duplicates except for the date, you mean that list.get(i) != list.get(j) and list.get(i).equals(list.get(j)).
If that's what you meant, then I believe the first one to get inserted into the set wins.
SLaks 已经在
HashSet
方面领先于我,但如果您可以选择使用LinkedHashSet
,那可能会更好。我对 javadoc for
LinkedHashSet
表示保证保留第一个副本:SLaks has already beaten me to the punch about
HashSet
, but if you have the option to useLinkedHashSet
instead, that may be better.My reading of the javadoc for
LinkedHashSet
indicates that the first duplicate is guaranteed to be preserved:就 Java 集合框架而言,
如果您只想在插入期间保留顺序,
HashSet
可以通过删除任何重复项来保留首次出现的位置。但是,如果您想在迭代期间保留顺序并使用LinkedHashSet
在 JPA 的情况下,
您可能会发现
select unique
查询对于过滤重复项很有用,从而避免冗余的 Collection 处理,请参阅< a href="https://stackoverflow.com/questions/263850/how-do-you-create-a-distinct-query-in-hql">如何在 HQL 中创建不同查询
In terms of java Collection Framework,
If you want to preserve the order only during insertion
HashSet
will do and keeps first occurrences there by removing any duplicates. However if you want to preserve order during iterations as well useLinkedHashSet
In case of JPA
you may find
select distinct
query useful to filter duplicates, there by avoiding redundant Collection processingsee How do you create a Distinct query in HQL