I do have int pairs, i.e; (int ,int)
1) Given k such pairs, check if they are unique. i.e; size of the Set formed using k pairs is k ?
2) if the given k records are unique then store them in sorted order ( by x and resolve conflict by y)
3) Given n such sets of size k, create a set of sets.
Example of requirement 1 and 2
if k = 3
(100, 100) (110, 300) (120, 200) is a valid set and in sorted order.
(100, 100) (300, 200) (200, 300) is a valid set but not in sorted order.
(100, 100) (100, 200) (100, 200) is in valid set
Example of requirement 3
input:
(100, 100) (200, 300) (300, 200)
(100, 100) (200, 300) (300, 200)
(100, 100) (201, 300) (300, 200)
output:
(100, 100) (200, 300) (300, 200)
(100, 100) (201, 300) (300, 200)
This is the closest analogy to the real problem i am facing. I need to get this done in Java and i have never worked in java. I am a intermediate c++ programmer.
I could solve 1 and 2 via some ugly coding and sorting.
However i am not able to get 3. Below is what i could get so far for 3. The class pair implements comparable
(poc code)
import java.util.HashSet;
public class set {
public static void main (String []args) {
HashSet<Pair> s1 = new HashSet();
s1.add(new Pair(10,10));
s1.add(new Pair(10,10));
HashSet<Pair> s2 = new HashSet();
s2.add(new Pair(10,10));
s2.add(new Pair(10,10));
HashSet<HashSet<Pair>> s12 = new HashSet();
s12.add(s1);s12.add(s2);
for ( HashSet<Pair> hs : s12) {
for (Pair p : hs) {
System.out.println(""+ p.toString());
}
}
}
}
Looks like you didn’t override
equalsand/orhashCodemethods in Pair class.For example if your
Pairclass has the following structure :You should implement
equalsandhashCodeas(example) :