Exclude null key from hashmap that is present inside arraylist

I am looking for an optimized solution to remove keys nullwith HashMap. This HashMap is present inside ArrayList. The following is an example.

public class Car {
    String year;
    String model;
    Map<String,String> colors;
}

List<Car> listOfCars = new ArrayList<Car>();

An example color map might look like this:

{
   red(1),
   blue(2),
   green(3),
   black(4),
   null(5)
}

I need a solution to iterate over listOfCars, get a color map and remove from it null. Trying to see other options in Java8 instead of using Iterator.

Thank!

+4
source share
1 answer

Considering that the card cannot contain duplicate keys, we can therefore say that each instance of the Mapinstance Carwill have only one record with the key null.

Java-8 forEach:

listOfCars.forEach(e -> e.getColors().remove(null));

for for.

+4

Source: https://habr.com/ru/post/1691651/


All Articles