-1
    Map<String, Object> m = new HashMap<>();
    ArrayList<String> str = new ArrayList<String>(Arrays.asList("Mohan", "Rohan", "", null));
    m.put("k1", str);
    m.put("k2", 43);
    m.put("k3", null);
    m.put("k4", "");
    System.out.println(m);
    Set<Map.Entry<String, Object>> entrySet = m.entrySet();
    Iterator<Map.Entry<String, Object>> itr = entrySet.iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Object> entry = itr.next();
        if (entry.getValue() == null || entry.getValue().toString().equals("")) {
            itr.remove();
        } else if (entry.getValue().getClass() == ArrayList.class) {
            ArrayList<String> arr = (ArrayList<String>) entry.getValue();
            for (int i = 0; i < arr.size(); i++) {
                if (arr.get(i) == null || arr.get(i).trim() == "") {
                    arr.remove(i);
                }
            }
        }
    }

I am getting stuck to remove empty and null values from arraylist Can anyone help me with this...

Udit Singh
  • 1
  • 1
  • 4
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – f1sh Mar 14 '22 at 09:49
  • @f1sh I think OP is struggling to remove elements from list(there is error in removal logic), not with string comparison. – Chaosfire Mar 14 '22 at 09:58
  • 1
    @Chaosfire OP's `arr.get(i).trim() == ""` is definitely a problem though. – f1sh Mar 14 '22 at 10:00
  • @f1sh You are right, it's definitely a problem as well, somehow i totally missed it. – Chaosfire Mar 14 '22 at 10:36
  • 1
    Why this inconsistency? At one place you’re only comparing with `""`, at the other place, you’re calling `trim()` before comparing. Besides that, don’t reinvent the wheel. Remove `null` and empty string from map: `m.values().removeAll(Arrays.asList(null, ""));` Remove the same from lists in the map: `m.values().forEach(o -> { if(o instanceof List) ((List>)o).removeAll( Arrays.asList(null, "")); });` – Holger Mar 14 '22 at 19:19

2 Answers2

1

Your problem is that you remove element at index i, then the collection size changes, but you don't update index. This causes you to skip elements. To fix it you need to decrement index after removal:

arr.remove(i);
i--;

But it's not good idea to change indexes during iteration, it's error prone. It would be better to:

  1. Make iteration backwards
for (int i = arr.size() - 1; i >= 0; i--)

Like this you don't need to change index when removing

  1. Better would be to use Iterator.remove(), the same way you are removing elements from map

  2. Probably easiest solution - use Collection.removeIf(Predicate)

arr.removeIf(str -> str == null || str.trim().isEmpty());

To citate java doc - Removes all of the elements of this collection that satisfy the given predicate.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
0

You could interrogate the map and remember items that match your criteria in a list. Then remove those items from the map like this:

Map<String, Object> m = new HashMap<>();
m.put("k3", null);
m.put("k4", "");

List<String> keysToRemove = new ArrayList();

m.forEach((key, value) -> {
  if (value == null ) {
    keysToRemove.add(key);
  }else if(value instanceof ArrayList){
    if(((ArrayList<String>) value).stream().filter(item -> item != null || !item.isEmpty()).count() == 0){
      keysToRemove.add(key);
    }
  }
});
for (String key : keysToRemove) {
  m.remove(key);
}
CKT
  • 383
  • 2
  • 8
  • 1
    Instead of `for (String key : keysToRemove) { m.remove(key); }` you can simply use `m.keySet().removeAll(keysToRemove);` – Holger Mar 14 '22 at 19:21