将列表传递给集合构造函数,过滤是否按顺序发生?

发布于 2024-12-16 23:42:23 字数 260 浏览 0 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

话少心凉 2024-12-23 23:42:24

如果您从包含重复项的集合创建 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.

殤城〤 2024-12-23 23:42:24

通过“重复条目”,您的意思是查询返回了不同的行/对象实例,并且您已经以不包含此 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.

榆西 2024-12-23 23:42:24

SLaks 已经在 HashSet 方面领先于我,但如果您可以选择使用 LinkedHashSet,那可能会更好。

我对 javadoc for LinkedHashSet 表示保证保留第一个副本:

请注意,如果将元素重新插入集合中,插入顺序不会受到影响。 (如果在调用之前 s.contains(e) 返回 true 时调用 s.add(e),则元素 e 会重新插入到集合 s 中。)

SLaks has already beaten me to the punch about HashSet, but if you have the option to use LinkedHashSet instead, that may be better.

My reading of the javadoc for LinkedHashSet indicates that the first duplicate is guaranteed to be preserved:

Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

爱她像谁 2024-12-23 23:42:24

就 Java 集合框架而言,

如果您只想在插入期间保留顺序,HashSet 可以通过删除任何重复项来保留首次出现的位置。但是,如果您想在迭代期间保留顺序并使用 LinkedHashSet

在 JPA 的情况下,

您可能会发现 select unique 查询对于过滤重复项很有用,从而避免冗余的 Collection 处理,

select distinct a from ....

请参阅< 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 use LinkedHashSet

In case of JPA

you may find select distinct query useful to filter duplicates, there by avoiding redundant Collection processing

select distinct a from ....

see How do you create a Distinct query in HQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文