2

I need a data set that contains pairs of values. And you can check whether a pair exists in such set regardless of the pair's key/value order, that is:

If the set contains

<1,5>
<8,3>

And you ask if it contains <5,1>, it will return true because the order does not matter.

I could write my own Pair class and design the hashCode() and equals() methods to account for my requirements, but I was wondering if Java has a library that serves this purpose already.

roeygol
  • 4,908
  • 9
  • 51
  • 88
Saturn
  • 17,888
  • 49
  • 145
  • 271
  • Plain vanilla Java doesn't. I think Guava provide such data structure. – Luiggi Mendoza Jan 16 '15 at 20:42
  • Sometimes, it takes less time to design your own wheel than it takes to find a re-useable wheel that fits. Your `Pair` class does not sound very hard to implement. It could be as simple as a decorator for a two-element `ArrayList` with a constructor that adds the smaller member first. – Solomon Slow Jan 16 '15 at 20:50
  • Graph data structure can work (the vertices). – Shahar Jan 16 '15 at 20:53

1 Answers1

0

I guess you already know the answer, on how to implement it, I don't think there is any library in java which can help you out for this requirement. So yes please implement your Pair class with similar implementation example given by patryk, in the previous answer.

Vikash Mishra
  • 635
  • 5
  • 7