javalistcollectionsimmutability

Converting Immutable to mutable list Java using new ArrayList<>, is there any alternative like using Stream, copy, etc.?


I have a method that returns an Immutable list. I want to add elements to it and that's why have to convert it to a mutable list. Currently, I am creating a new ArrayList out of the Immutable list as follows:

final List<someDTO> mutableList = new ArrayList<>(someDTO.getImmutableList());

Is there any better way of doing it like using some collections copy method, java streams, or anything like that?


Solution

  • in all honesty, that's as best as it gets, but another variant would be:

    someDTO.getImmutableList().stream().collect(toCollection(ArrayList::new));