7

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)

Oreo
  • 2,586
  • 8
  • 38
  • 63
  • equals method compare to object. Here you are Comparing list object with list item which is wrong. You need to iterate through list to check if particular name exist or not. – Dhaval Patel May 02 '15 at 12:53
  • Error in **condition**. Print Value of `DataArrayList.secondArraylist` and `strName` You will find the error. – Paresh P. May 02 '15 at 12:55
  • Check out : http://stackoverflow.com/questions/14192532/how-to-prevent-the-adding-of-duplicate-objects-to-an-arraylist – Haresh Chhelana May 02 '15 at 13:04

4 Answers4

24

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)");
}
pathfinderelite
  • 3,047
  • 1
  • 27
  • 30
  • looks good, but can you show me same using some other way, might be using more easy way – Oreo May 02 '15 at 12:58
  • my mean something like this: for (int i = 0; i < DataArrayList.secondArraylist.size() ; i++) { if (i.contains(strName)) { System.out.println("Not Exist(s)"); } else { System.out.println("Data Exist(s)"); } } – Oreo May 02 '15 at 13:01
  • getting: The method contains(ArrayList, String) is undefined for the type new View.OnClickListener(){} – Oreo May 02 '15 at 13:04
  • thank you so much, what if i want to remove item if already exists in ArrayList, finally using this: http://stackoverflow.com/a/30023021/4675078 – Oreo May 04 '15 at 05:28
  • This is not the *optimized* way to check whether object already exists in Array. why to iterate the array when you already have _in built function contains_ ? – SweetWisher ツ May 04 '15 at 05:46
  • @SweetWisherツ The built-in `contains` method will also iterate over the items - its still a array, not a hash. – pathfinderelite May 04 '15 at 12:21
2

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)");
}
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • This assumes that two `Second` objects are equal if their names are equal. I don't think this is a safe assumption. – pathfinderelite May 04 '15 at 12:22
  • This is not the assumption. _This is what he needs_ we can return the value from `toString` on which we wanna compare two objects – SweetWisher ツ May 04 '15 at 12:51
  • Where does `toString()` come into the picture? It's not used for comparison. Also, why `!super.equals(obj);`? That would cause the `equals` method to do the opposite of its intent. – pathfinderelite May 04 '15 at 13:08
  • @pathfinderelite First, get clear with concept of _contains_ method. You'll get all answers :) – SweetWisher ツ May 05 '15 at 04:39
0

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)");
}
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
0

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.

Gisonrg
  • 519
  • 4
  • 13