ArrayList checks if data exists

I just want to check if the data already exists or not in the ArrayList, but each time it turns out: Not Exist (s), while the data exists in the ArrayList, and as a result I get duplicate entries in the list.

    DataArrayList.secondArraylist.add(new Second(actorList.get(position).getName(), actorList.get(position).getImage()));
             System.out.println(DataArrayList.secondArraylist.size());   

             String strName = actorList.get(position).getName().toString();
             Log.d("name:", strName);

             for(int i=0; i<DataArrayList.secondArraylist.size(); i++) 
             {

                 if(DataArrayList.secondArraylist.get(i).getName().equals(strName)) {
                     System.out.println(DataArrayList.secondArraylist.get(i).getName());

                     Toast.makeText(context, "Exist(s)", Toast.LENGTH_SHORT);
                }
                 else {

                     Toast.makeText(context, "Not Exist(s)", Toast.LENGTH_SHORT);
                }

             }

Problem:

Do not receive Toast, which I use to indicate that "Data Exists" or "Not"

Finally:

I would like to add an element to arraylist if it already does not exist, otherwise you want to show Toast that the element already exists

+4
source share
4 answers

You are comparing your list object DataArrayList.secondArraylistwithString

if (DataArrayList.secondArraylist.equals(strName)) {

false. , strName Second, .

boolean contains(ArrayList<Second> list, String name) {
    for (Second item : list) {
        if (item.getName().equals(name)) {
            return true;
        }
    }
    return false;
}

if (contains(DataArrayList.secondArraylist, strName)) {
    System.out.println("Data Exist(s)");
} else {
    System.out.println("Not Exist(s)");
}
+14

contains equals equals (Object), POJO.

equals (Object), hashcode()

:

@Override
public String toString() {
    return getName();
}

 @Override
 public boolean equals(Object obj) {
     return !super.equals(obj);
 }

 @Override
 public int hashCode() {
      return getName().hashCode();
 }

:

, "". , :

 (DataArrayList.secondArraylist.contains(actorList.get(position))) 

:

if (DataArrayList.secondArraylist.contains(actorList.get(position))) {
    System.out.println("Data Exist(s)");
} else {
    System.out.println("Not Exist(s)");
}
+1

indexOf(), ArrayList

if (DataArrayList.secondArraylist.indexOf(strName)!=-1) {
System.out.println("Data Exist(s)");
    } else {
System.out.println("Not Exist(s)");
}
0

Since the strNameone you are checking is a property of your object Second, you cannot check it directly using the method ArrayList. The easiest way is to loop through an ArrayList DataArrayList.secondArraylist, retrieve each Secondobject in the list, and then check to see if strNameyour property matches this property Second.

You don’t know how you implement the object Second, but you should get the corresponding string from each object Secondand then compare it with strName.

0
source

All Articles