Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best structure for non-duplicate pairs?

 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

I am in need of finding the best way to store a bunch of paired int values that can not be duplicates. For example, I can have a {3, 3} and a {3, 4} and a {4, 3}, but I will never have more than one of each of those.

Is there some kind of inherent structure in Java that would be ideal for me to use?

Thank you very much!
bryan
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can use the Set object to store your pair. This will assure the non-duplicate. then you need to code your Pair structure (a class with two field and you need to override the equals() and hashCode() methods and implements the Comparable interface

the coding of those methods is left as an exercise.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you can use java.awt.Point for your pairs.
Also, I don't see an immediate need for pairs to be
Comparable, certainly HashSet doesn't care.
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking that a Set using java.awt.Point objects would be just about perfect!

Thank you both!

bryan
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set is perfect. There is already an implementation in the form of HashSet. You can control its behavior in how you implement equals and hashcode. read Object to find out how.

Implement Comparable if you want to be able to sort the set with something like Collections.getSortedSet(Set s) or some such, as by default Sets are not ordered.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic