How to sort a Set?

L

laredotornado

Hi,

I have a Set of Strings for which I'd like to sort, so that when I
iterate over the set, I'm doing so in sorted order.

Hashtable<String,Hashtable<String,String>> apps = getApps();
java.util.Set<String> allowedApps = apps.keySet();
Iterator<String> i = allowedApps.iterator();

How can I sort the data? Thanks, - Dave
 
M

Mark Space

laredotornado said:
Hi,

I have a Set of Strings for which I'd like to sort, so that when I
iterate over the set, I'm doing so in sorted order.

Hashtable<String,Hashtable<String,String>> apps = getApps();
java.util.Set<String> allowedApps = apps.keySet();
Iterator<String> i = allowedApps.iterator();

How can I sort the data? Thanks, - Dave

I'd use allowedApps.toArray( String[] ) and then sort the resulting
array. Look at java.util.Arrays.sort().
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark Space schreef:
| laredotornado wrote:
|> Hi,
|>
|> I have a Set of Strings for which I'd like to sort, so that when I
|> iterate over the set, I'm doing so in sorted order.
|>
|> Hashtable<String,Hashtable<String,String>> apps = getApps();
|> java.util.Set<String> allowedApps = apps.keySet();
|> Iterator<String> i = allowedApps.iterator();
|>
|> How can I sort the data? Thanks, - Dave
|
| I'd use allowedApps.toArray( String[] ) and then sort the resulting
| array. Look at java.util.Arrays.sort().

Why? What’s wrong with Collections.sort()?

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkiHYBMACgkQBGFP0CTku6MlwwCgi9OPgyv7S3DB8oiDknH3ZUmE
sgYAn1YTpSHM1dLsIIe3mdoT9IhZL4kn
=9CZl
-----END PGP SIGNATURE-----
 
M

Mike Schilling

laredotornado said:
Hi,

I have a Set of Strings for which I'd like to sort, so that when I
iterate over the set, I'm doing so in sorted order.

Hashtable<String,Hashtable<String,String>> apps = getApps();
java.util.Set<String> allowedApps = apps.keySet();
Iterator<String> i = allowedApps.iterator();

How can I sort the data? Thanks, - Dave

Youi could use a TreeMap instead of a Hashtable, and the iterator
would return the keys in sorted order automatically.
 
L

laredotornado

laredotornadowrote:




Youi could use a TreeMap instead of a Hashtable, and the iterator
would return the keys in sorted order automatically.

Is there a simple way to convert a Hashtable to a TreeMap? The code
that generated the Hashtable is not my own and I'm not free to change
it. - Dave
 
L

Lew

Youi could use a TreeMap instead of a Hashtable, and the iterator
would return the keys in sorted order automatically.

One should avoid Hashtable unless one, a) needs synchronization and,
b) needs all that extra non-Collections cruft.

The performance characteristics of TreeMap might be suboptimal. If
the need for sorted access is sufficiently rare, or the underlying Map
changes sufficiently infrequently, one could use idioms along the
lines of

SortedMap <String, String> apps = new TreeMap <String, String>
( getApps() );

or

Map <String, String> apps = getApps();
SortedSet <Map.Entry <String, String>> sortedEntries =
new TreeSet <Map.Entry <String, String>> ( apps.entrySet() );

or

Map <String, String> apps = getApps();
SortedSet <String> sortedKeys = new TreeSet <String>
( apps.keySet() );
 
M

Mark Space

Roedy said:
Ouch, yes. Why then is it in Collections?

List is one type of Collection?

I was trying to reply to the OPs issue, which really concerned
Hashtables. That's what he has. There might be some more efficient way
of sorting the keys (?) in a Hashtable, I guess. I just presented what
I thought was the most obvious answer. The OP could nose around the
docs for the objects I mentioned, see if anything looked better, and do
some performance testing.

Or if performance doesn't matter, then just take the first solution that
presents itself. It's really dependent on parameters we don't have at
the moment.
 
C

Christian

Roedy said:
Collections.sort and Collections.binarySearch should be deprecated and
replaced by Lists.sort and Lists.binarySearch.
Though the class is Collections not Collection

And List is part of the Collections framework .. as this Collections is
a nice name to contain all static methods for the Collections framework.
I see no need for a deprecation as the naming seems to be intuitive and
practical.

Christian
 
J

John W Kennedy

Roedy said:
Ouch, yes. Why then is it in Collections?

On the one hand, Lists are collections. On the other hand, HashSets
cannot be sorted, and TreeSets already are. In order to sort a Set,
therefore, you /must/ create a List (or an Array) and then sort it. It's
not a software restriction; it's a basic logical consequence of what a
Set /is/.
 
C

Christian

John said:
On the one hand, Lists are collections. On the other hand, HashSets
cannot be sorted, and TreeSets already are. In order to sort a Set,
therefore, you /must/ create a List (or an Array) and then sort it. It's
not a software restriction; it's a basic logical consequence of what a
Set /is/.
not really .. you coulkd define a Order on a set..
or implement a set based on ArrayList.. (CopyOnWriteArrayList has
already the addIfAbsent Method implemented which you need)

its a basic logical consequence of what a HashMap is.
If you would want to define also an Order it would be a size overhead ...
as you can't just use your array holding the elements for hashbuckets
that induce some sort of order ... and also a self defined order..

So it is a software restriction.
 
L

Lew

laredotornado said:
Is there a simple way to convert a Hashtable to a TreeMap?

The code that generated the Hashtable is not my own
and I'm not free to change it.

Are there curse issues?

Hashtable has been configured by book worm alternatives since about 1998.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"We cannot let terrorists and rogue nations
hold this nation hostile or hold our allies hostile."

--- Adolph Bush, Skull and Bones initiate

In an August 7, 2000 Time magazine interview,
George W. Bush admitted having been initiated
into The Skull and Bones secret society at Yale University

"...these same secret societies are behind it all,"

my father said. Now, Dad had never spoken much about his work.

--- George W. Bush
 
J

John W Kennedy

Christian said:
not really .. you coulkd define a Order on a set..

....which TreeSet already is.
or implement a set based on ArrayList.. (CopyOnWriteArrayList has
already the addIfAbsent Method implemented which you need)

Have you actually read the documentation for CopyOnWriteArrayList?

Very well. I misspoke. I should have said "it's a basic logical
consequence of what a Set is when implemented by someone sane enough not
to do it in O(n^2)."
 
C

Christian

John said:
...which TreeSet already is.


Have you actually read the documentation for CopyOnWriteArrayList?

Very well. I misspoke. I should have said "it's a basic logical
consequence of what a Set is when implemented by someone sane enough not
to do it in O(n^2)."

you could for example without to large overhead do a set with a order by
simply holding 2 arrays in the class.. one for the hashbuckets and one
for the order ...
Other way would be to introduce a next pointer in the Entry objects that
points to the next Object in order

there is really no reason what so ever for an ordered set to perform
asymptotically worse then the normal hashset..

Actually Java already has such a Set that does this called LinkedHashSet.

Christian
 
R

Roedy Green

On the one hand, Lists are collections. On the other hand, HashSets
cannot be sorted, and TreeSets already are. In order to sort a Set,
therefore, you /must/ create a List (or an Array) and then sort it. It's
not a software restriction; it's a basic logical consequence of what a
Set /is/.

My complaint is not that you can't order a Collection, but that sort
is part of the Collections class. There should have been a Lists
class created to hold sort and binarySearch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top