简体   繁体   中英

ArrayList check if data already exists

Just want to check if data already exist(s) or not in ArrayList, but everytime getting : Not Exist(s) whereas data exists in ArrayList, and as a result i am getting duplicate records in a 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:

Not getting any Toast message, which i am using to indicate that "Data Exists" or "Not"

Finally:

I would like to add item to arraylist if already not exist(s), else want to show Toast that item already exist(s)

You are comparing your list object DataArrayList.secondArraylist with a String

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

That will always return false . Instead, create a method that loops through the list and checks the strName against the name of the Second objects that are stored in the list.

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

Then use it

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

The contains method calls the equals method with signature equals(Object), so you requires to override this method in your POJO class.

If you override equals(Object) then you should also override hashcode()

Code snippet :

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

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

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

Edit :

Moreover, The contains method is used for Object. So, your condition should look like this :

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

Final code :

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

You need to use indexOf() while checking a value in ArrayList

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

As the strName you are checking is a property of your Object Second , you cannot check it directly using method of the ArrayList . The easiest way would be loop through the ArrayList DataArrayList.secondArraylist , get each Second object contained in the list, then check if strName is equal to that property of your Second object.

Not sure how you implement the Second object, but you should get the corresponding String from each Second object then compare it with the strName .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM