嗨,我正在尝试实现一个基于SQLite的应用程序,并在我的数据库表中有一些值。我的专栏之一包含一些相似的值,并且为了克服重复,我使用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;如果有重复项,则set应该遵循其属性以避免重复项,但是它给我相同的值,但对象不同。更新:这是我的模特班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)@Overridepublic 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)@Overridepublic int hashCode() { return Objects.hash(getPro_id());}}Image of the Database (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我试过了您的代码,如果您删除了注释,它似乎可以正常工作。我的猜测是因为有注释:@RequiresApi(api = Build.VERSION_CODES.KITKAT)方法equals和hashCode没有被注入。我强烈建议删除这些注释,因为equals和hashCode绝不是Kitkat API的专有部分。自Java诞生以来,它们就已经存在了。 (adsbygoogle = window.adsbygoogle || []).push({});
08-05 10:43