1

Hi i am trying to implement a SQLite based Application and have some values in the table of my database. one of my column contains some similar values and to overcome the duplication , I used set<>

public Set<NewTableAnswers> newtableAnswers(String product_id) {
    Set<NewTableAnswers> d = new HashSet<>();
    SQLiteDatabase db = this.getReadableDatabase();

    String SelectA1 = "SELECT * FROM " +TABLE_ANSWERS+ " WHERE " + ANSWER_ID + " = '"+ product_id + "' ";
    Cursor cursor = db.rawQuery(SelectA1,null);
    if (cursor.moveToFirst()){
        do{
            d.add(new NewTableAnswers(cursor.getString(1)));
        }while (cursor.moveToNext());
    }
    db.close();
    return d;

if there is a duplicate, set should follow its property to avoid duplicates, but its giving me same value with different objects..like this...

Different objects for same value

Update:

this is my model class..

public class NewTableAnswers {

String pro_id;

public NewTableAnswers(String pro_id) {
    this.pro_id = pro_id;
}

public String getPro_id() {
    return pro_id;
}

public void setPro_id(String pro_id) {
    this.pro_id = pro_id;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof NewTableAnswers)) return false;
    NewTableAnswers that = (NewTableAnswers) o;
    return Objects.equals(getPro_id(), that.getPro_id());
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public int hashCode() {

    return Objects.hash(getPro_id());
}

}

Image of the Database

rituparna
  • 91
  • 1
  • 12
  • 7
    Did you override equals and hashCode? – Eran Apr 17 '18 at 07:30
  • 1
    for that you have to implement equals and hashcode properly – Blackbelt Apr 17 '18 at 07:30
  • 2
    Possible duplicate of [Why do I need to override the equals and hashCode methods in Java?](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) – HBo Apr 17 '18 at 07:31
  • 3
    We can't help you without seeing the implementation of `NewTableAnswers`, in particular (as @Eran said) `equals` and `hashCode`. Read the `Set` documentation for why. – T.J. Crowder Apr 17 '18 at 07:31
  • 2
    FYI: Eran and Blackbelt mean that you should implement those methods in class `NewTableAnswers` – Jack Flamp Apr 17 '18 at 07:31
  • My newTableAnswers class has just one string variable with a constructor and its getter,setter – rituparna Apr 17 '18 at 07:34
  • That is not relevant, override equals and hashCode, or just use a Set of Strings – Scary Wombat Apr 17 '18 at 07:35
  • Provide a [mcve] not using the DB but a list of `String` to show the problem. This could be a simple trail of space messing with you. _PS: please check what is a SQL Injection. This is not a safe code._ – AxelH Apr 17 '18 at 08:03

1 Answers1

1

I have tried you code and it seems to work fine, if you remove the annotations.

My guess is that because of the annotations:

@RequiresApi(api = Build.VERSION_CODES.KITKAT)

the methods equals and hashCode are not being injected. I strongly suggest to remove these annotations, because equals and hashCode are by no means exclusive part of the API of Kitkat. They have been around since the very beginning of Java.

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • but now i am getting only one item in the set. so the size is always 1. i have other elements too.. – rituparna Apr 17 '18 at 09:22
  • @rituparna good. So removing the annotation solved one problem. If you are getting one element, even though you have multiple answers - this means that you have not defined the key properly in your code. You have now to ask yourself: which are the fields out of which the key is composed? Then you need to include these fields in your `NewTableAnswers` class and then change accordingly the `hashCode` and `equals` method to include these key fields. – gil.fernandes Apr 17 '18 at 09:29
  • @rituparna so if the image has a name and belongs to the key (*key* is what allows you to identify an object) then include the image name in the key and `hashCode` and `equals` method. This will hopefully then give you the right results. – gil.fernandes Apr 17 '18 at 09:41
  • if you see in code, i have used ANSWER_ID in my database query. this answer id is the proid. like private static final String ANSWER_ID = "proid"; . now i have an argument passing in the whole method. this argument is passing the parameters from a loop running for an arraylist..duh!! i am bad at explaination...sorry – rituparna Apr 17 '18 at 09:46