Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

adding unordered pairs without duplicates

Thread view: 
bluedoze@yahoo.com - 27 Jun 2005 14:27 GMT
Hello,

I want to construct a list of pairs without dupliactes, not the key
value pairs, I just want to ensure that the pair (not ordered) is not
dupliacted in the list.

for example

method input: (3, 4), (5, 6), (4, 3), (6, 5)

method output: (3, 4), (5, 6).

I can do this manually, but is there any utility in the java language
itself ?

Bluedoze
Ingo R. Homann - 27 Jun 2005 15:02 GMT
Hi bluedoze,

> Hello,
>
[quoted text clipped - 12 lines]
>
> Bluedoze

I would use a normal (Hash)Set and a class "UnorderedPair" that
overrides equals and hashCode in the desired manner. Perhaps, it's
useful to store the two values as a "min" and a "max".

Hth,
Ingo
bluedoze@yahoo.com - 27 Jun 2005 15:11 GMT
it's actually not numbers, so I cannot use min and max.

any way thanks for the replies.
Ingo R. Homann - 27 Jun 2005 15:43 GMT
Hi,

> it's actually not numbers, so I cannot use min and max.
>
> any way thanks for the replies.

Well, it must not be numbers to use min and max. That's also possible
for Strings, for example, indeed for all classes that implement Comparable.

Even, if the 'values' do not implement Comparable, it should be possible
to write a proper Pair-class.

Ciao,
Ingo
Daniel Dyer - 27 Jun 2005 15:03 GMT
> Hello,
>
[quoted text clipped - 10 lines]
> I can do this manually, but is there any utility in the java language
> itself ?

If you use a collection that does not allow duplicates (such as a HashSet,  
TreeSet or other Set implementation) then, given an appropriate  
implementation of the equals() method (in your Pair class), any duplicates  
will be discarded by the collection when you add them.

Beware, this is one of those situations where you really do need to  
over-ride hashcode() when you override equals().

Dan.

Signature

Daniel Dyer
http://www.footballpredictions.net



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.