I have written a simple test program in which I am trying to store unique pair of (String,String). Here below I mentioned my code part:
public class Pair {
private String cr_ts_hi;
private String cr_ts_lo;
//constructor and getter-setter
}
class Test{
private static HashSet<Pair> caseExceptionReport = new LinkedHashSet<Pair>();
public static void main(String[] args) {
caseExceptionReport.add(new Pair("abc","itm1"));caseExceptionReport.add(new Pair("abc","itm2"));
caseExceptionReport.add(new Pair("abc","itm1"));caseExceptionReport.add(new Pair("def","itm1"));
caseExceptionReport.add(new Pair("def","itm2"));caseExceptionReport.add(new Pair("def","itm2"));
caseExceptionReport.add(new Pair("xyz","itm1"));caseExceptionReport.add(new Pair("xyz","itm2"));
for(Pair m:caseExceptionReport){
System.out.println(m.getCr_ts_hi() + " *** " + m.getCr_ts_lo());
}
}
And output is:
abc *** item1
abc *** item2
abc *** item1
def *** item1
def *** item2
def *** item2
xyz *** item1
xyz *** item2
Expected output is:
abc *** item1
abc *** item2
def *** item1
def *** item2
xyz *** item1
xyz *** item2
I am not getting a way to store unique pairs. I though HashSet won't allow duplicate Pair's but it is not working. Any other idea for this?