Sorting Components

J

John

I need to store hundreds of Components (a real mix of subclasses) in an
array/list, and need to be able to search the array for the presence of
a given Component . I therefore need to sort the array by some key which
is unique among components to avoid wasting lots of time doing equals()
comparisons.

At the moment I am considering sorting by Object.hashCode() and dealing
with conflicts by using equals() comparisons.

Does anyone have a better suggestion for doing this?
 
M

Matt Humphrey

John said:
I need to store hundreds of Components (a real mix of subclasses) in an
array/list, and need to be able to search the array for the presence of
a given Component . I therefore need to sort the array by some key which
is unique among components to avoid wasting lots of time doing equals()
comparisons.

By what key are you searching for them? If you're only testing for existence
in the list / set, use a HashMap (e.g. searching by object reference). If
you're trying to find one based on other key values (name, etc) use one or
more TreeMap as indexes, each using a different key.
At the moment I am considering sorting by Object.hashCode() and dealing
with conflicts by using equals() comparisons.

Does anyone have a better suggestion for doing this?


Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
R

Roedy Green

I need to store hundreds of Components (a real mix of subclasses) in an
array/list, and need to be able to search the array for the presence of
a given Component . I therefore need to sort the array by some key which
is unique among components to avoid wasting lots of time doing equals()
comparisons.

Have a look at the source for Accumulate at
http://mindprod.com/products.html#ACCUMULATE

It extracts the keys from the HashMap and sorts them when needed.
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top