2

I have an table in database that contains 3 values did, pid and fid. which is something like below:

  ---------------------
  | did  | fid |  pid |
  --------------------|
  | 259  | 431 | 128  |
  --------------------|
  | 259  | 431 | 132  |
  |-------------------|
  | 260  | 432 | 128  |
  --------------------|
  | 260  | 432 | 129  |
  |-------------------|
  | 260  | 432 | 132  |
  |-------------------|
  | 261  | 460 | 128  |
  --------------------|
  | 261  | 460 | 132  |
  |-------------------|

Now if any new entry comes up. I want to check in table where it exists or not. None of them is unique. In the response one must be uniuqe all of them can not be same for new entry.

I am using currently double for loop which is not working.

Thanks in advance

EDITED

public class DetailingTripleCheck {

private int nid;
private int did;
private int fid;

public DetailingTripleCheck() {
    // TODO Auto-generated constructor stub
}

public DetailingTripleCheck(int nid, int did, int fid) {
    this.nid = nid;
    this.did = ddnid;
    this.fid = fid;
}

public int getNid() {
    return nid;
}

public void setNid(int nid) {
    this.nid = nid;
}

public int getDid() {
    return did;
}

public void setDid(int did) {
    this.did = did;
}

public int getFid() {
    return fid;
}

public void setFid(int fid) {
    this.fid = fid;
}

@Override
public boolean equals(Object o) {
    // TODO Auto-generated method stub
    return super.equals(o);
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    return super.hashCode();
}

}

Where i am using this class:

for (int i = 0; i < NewDids.length; i++) {
                    if (OldDids.length > 0) {
                        DetailingTripleCheck ddSet = new DetailingTripleCheck();
                        ddSet.setDdnid(Integer.parseInt(NewDids[i]));
                        ddSet.setProdNid(Integer.parseInt(NewNids[i]));
                        ddSet.setFid(Integer.parseInt(Newfids[i]));
                        HashSet<DetailingTripleCheck> set = new HashSet<DetailingTripleCheck>();

                        for (int j = 0; j < OldDids.length; j++) {
                            DetailingTripleCheck triple = new DetailingTripleCheck(
                                    Integer.parseInt(OldDids[j]),
                                    Integer.parseInt(OldNids[j]),
                                    Integer.parseInt(OldFids[j]));
                            if (set.contains(triple)) {
                                System.out.println("TRUE");


                            } else {
                                System.out.println("FALSE");


                            }
                       }
}
user3154663
  • 291
  • 1
  • 2
  • 18
  • Create a new class `Triple` that contains the 3 fields, [override `hashCode()` and `equals()`](http://stackoverflow.com/q/27581/572670), and store the table in a [`HashSet`](http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html). – amit Jul 19 '14 at 09:22
  • @amit thanks for yours swift response. Could you please give me some example. – user3154663 Jul 19 '14 at 09:23

1 Answers1

1

Assume the fields are ints (easy to modify if the assumption is wrong).

Create a new class, call it Triple, that contains the 3 fields:

class Triple { 
  int did, fid, pid;
  Triple(int did, int fid, int pid) { 
     this.did = did;
     this.fid = fid;
     this.pid = pid;
  }
  ... 
}

Make sure you override hashCode() and equals() (Eclipse IDE can help you to do it automatically).

Now, store your data in a HashSet:

HashSet<Triple> set = new HashSet<>();

Once an element is added, check if it already exists using the contains() method, and if it's not - add it to the set.

Triple triple = new Triple(did,fid,pid)
if set.contains(triple) { 
   //do something
} else { 
   set.add(triple) 
}
Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
  • What should be inside hashCode and equals method? public boolean equals(Object o) { // TODO Auto-generated method stub return super.equals(o); } public int hashCode() { // TODO Auto-generated method stub return super.hashCode(); } – user3154663 Jul 19 '14 at 09:36
  • @user3154663 If you are using eclipse IDE, you can auto generate them using Source -> Generate hashCode and equals() ... – amit Jul 19 '14 at 09:46
  • any logic that i have to put inside that or only above code that i have pasted in comment. – user3154663 Jul 19 '14 at 10:10
  • @user3154663 You need to create hashCode and equals with significant logic, of course. Use eclipse automatic generation for it. – amit Jul 19 '14 at 11:11
  • which class i have to override hashCode and equals method – user3154663 Jul 20 '14 at 06:53
  • @user3154663 The `Triple` class we just introduced. – amit Jul 20 '14 at 06:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57620/discussion-between-user3154663-and-amit). – user3154663 Jul 20 '14 at 07:22