3

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!

DataNucleus
  • 15,199
  • 3
  • 30
  • 37
notbrain
  • 3,166
  • 2
  • 28
  • 42

4 Answers4

4

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.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
1

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.

wrschneider
  • 15,487
  • 10
  • 70
  • 152
0

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.)

Daniel Pryden
  • 54,536
  • 12
  • 88
  • 131
0

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

Community
  • 1
  • 1
Prashant Bhate
  • 10,130
  • 7
  • 42
  • 81