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...
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());
}
}