0

I have a custom ArrayList and String array. What I want is to remove a row in ArrayList which is match with the value of the String array.

My ArrayList.....

ArrayList<SqlFavHeaderData> hList = db.getAllHeaderDetails();

SqlFavHeaderData

private String mId;
private String mType;
private double mPrice;
private String mBeds;

String Array....

String[] ss = new String[i];

This has values that match with mId in the ArrayList.

What I want is to remove items from ArrayList comparing with mId, that matched the values in the String Array.

I tried like this.But no luck...

   for (int i = 0; i < 10; i++) {
        hList.remove(ss[i]); 
    }
D.madushanka
  • 429
  • 6
  • 17
  • 1
    You need to implement a meaningful version of `equals()` for your class if you want to use such collection methods. Beyond that: learn about java naming conventions. that hungarian notion `mSomething` is not a recommended practice in java. And use meaningful names in the first place. ss sure isn't. – GhostCat Apr 15 '19 at 06:36
  • try this `ArrayList hList = = db.getAllHeaderDetails(); String[] ss = new String[i]; for (int i = 0; i < hList.size(); i++) { if (hList.get(i).equalsIgnoreCase(ss[i])){ hList.remove(ss[i]); } }` – Hemant Parmar Apr 15 '19 at 06:39

1 Answers1

2

Try this It will remove all the items that have mID similar to your string array.

    for (int i=0; i< hList.size(); i++) {
        for (String mID: ss) {
            if (hList.get(i).getmId().equals(mID)) {
                hList.get(i).remove(i);
            }
        }
    }

If you are using java8 then you can use:

      for(String mid:ss) {
            hList.removeIf(sqldata->sqldata.getmId().equals(mid));
        }
gprasadr8
  • 789
  • 1
  • 8
  • 12
Dipakk Sharma
  • 976
  • 1
  • 8
  • 11