I am new to Java streams and functional programming and I wonder if my question will ever even have a valid use case or it is just not meaningful. I have the following code that gives me a Set from an array
public static void main(String[] args) {
String []arr = {"a","abc","b","cd"};
Set<String> dict = new HashSet<>();
Arrays.stream(arr).map(item->{
System.out.println(item);
dict.add(item);
return dict;
});
// dict = Arrays.stream(arr).collect(Collectors.toSet());
System.out.println(dict);
}
This gives me an empty dict. If I do it the way I have in second last line, it works. Is there a way to mutate already instantiated dict
that was instantiated outside stream that it gets populated in the map
function?