-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Best way to distinct an *ordered* list?
PostPosted: Tue May 17, 2005 12:12 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 4:28 pm
Posts: 44
(Using 2.1.7)

I have a method making lists of objects distinct by using the suggested new HashSet(originalList) technique... Unfortunately when I've ordered my query in HQL, I believe I'm at risk of losing the order of my elements (since Set doesn't guarantee order).

Has anyone suggested an elegant/efficient method of return a distinct List which retains the order of the original one? I imagine I could write something fairly simple that tromps through the list and deletes duplicates. I wanted to make sure there wasn't a better solution I'd overlooked though.

Also, I don't believe SortedSet implementations will work for me because they require their elements implement comparable. I suppose I could have my entities implement Comparable, but this wouldn't be sufficient for me since I might sort them by a number of different fields for different queries.

Thanks in advance,

Phill


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 6:29 pm 
Newbie

Joined: Mon Jun 07, 2004 9:18 am
Posts: 13
Location: Torino, Italy
Have you already tried this ?

Code:
new LinkedHashSet(yourList);


Simon[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 7:17 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 4:28 pm
Posts: 44
Wow. I'd overlooked that because it doesn't implement SortedSet. I'm still confused by the documentation as to whether or not I can trust it to retain the order of my original collection. I don't see why it wouldn't work though. It is a bit faster than the other implementation we worked out today, which was:


HashSet set = new HashSet();
ArrayList results = new ArrayList(originalResults.size());

Iterator it = originalResults.iterator();
while (it.hasNext()) {
Object item = it.next();
if (!set.contains(item)) {
set.add(item);
results.add(item);
}
}

return results;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.