Question: I have parsed through a CSV file and stored it as an array of strings. I'd like to add an item to each sub-array if certain conditions have been met, as shown below. However, I am not able to use the `.add()` method and get a "Cannot resolve method" message in my IDE. To try to get around this, I attempted to create a new arraylist in which I placed the contents of the string array, but the problem persisted. How do I make it so that I can add an item to each sublist?

```html
import java.util.List;
import java.util.ArrayList;

public class Application {
    /**
     * Main entry of the application.
     *
     * @param args This should be empty
     */
    public static void main(final String[] args) {
        String csvFile = "/IUCNListV2.csv";
        List<String[]> listAnimal =
        ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));

        List<String[]> list2 = new ArrayList<>();
        for (String [] text : listAnimal) {
            list2.add(text);
        }

        for(String[] subList: list2)
            if (null != subList && subList[4].equals("x") &&
                    subList[5].equals("y")) {
                subList.add("z");
            }
    }
}
```

Answer: 
I agree with @Nikolas approach except that you should not return `null`, returning `null` at last is against using `Optional`
What about this one:

```
Optional<College> optional = Optional.ofNullable(student)
                .map(stud -> stud.getCollegeList())
                .orElse(Collections.emptyList())
                .stream()
                .filter(c -> Objects.nonNull(c.getCollegeName()))
                .findFirst();

```
Calling `Optional::get` with no previous check `Optional::isPresent` is dangerous because it might produce `CollegeNotFoundException`. And it is not the way the `Optional` shall be used. The idea of `Optional` is mapping/filtering the values and providing a *default* value if the `Optional` ends up with no element (empty).

Assuming `Student::getCollege` returns `List<College>` having method `College::getCollegeName`, you can do the following:

```
College college = Optional.ofNullable(student)
    .map(stud -> stud.getCollege())
     // if Optional is empty, then use an empty collection
    .orElse(Collections.emptyList())                              
    .stream()
    .filter(c -> Objects.nonNull(c.getCollegeName())) 
    .findFirst()
     // get the value or else college is null 
    .orElse(null);

```

As long as `stud.getCollege()` returns `null`, the `Optional` becomes empty and an empty list will be streamed. And again the same principle is applied: As long as the list is empty, the `filter` and `findFirst` are not be called and `null` is safely returned (or any default value you wish).

Also note that the line `.filter(c -> Objects.nonNull(c.getCollegeName()))` might also produce `NullPointerException` as long as there is not guaranteed `stud.getCollege()` doesn't return a list with a `null` element (remember the list is not `null` itself so `Optional` treats it as a "valuable" item). The safe code actually looks like:

```
Optional<College> college = Optional.ofNullable(student)
    .map(stud -> stud.getCollege())
    .orElse(Collections.emptyList())
    .stream()
    .filter(c -> c != null && c.getCollegeName() != null)
    .findFirst();

```

Actually, I prefer to return either a null-object, `null` or `Optional` itself.