The following code:
names = Arrays.asList("A","B","C").stream();
List<String> namesAsList = names.collect(() -> new ArrayList<String>(),List::add,List::add);
System.out.println("Individual Strings put into a list: " + namesAsList);
generates the following error during compilation:
List namesAsList = names.collect(() -> new ArrayList(),List::add,List::add); ^ (argument mismatch; invalid method reference incompatible types: ArrayList cannot be converted to int) where R,T are type-variables: R extends Object declared in method collect(Supplier,BiConsumer,BiConsumer) T extends Object declared in interface Stream 1 error
When I amend the code to remove the generic the code compiles with an unchecked expression warning:
Stream<String> names = Arrays.asList("A","B","C").stream();
List<String> namesAsList = names.collect(() -> new ArrayList(),List::add,List::add);
System.out.println("Individual Strings put into a list: " + namesAsList);
Why would I be receiving this error? I do not expect the problem to be relating to an int.
If the answer could include the way of figuring out the issue, this will be appreciated, so I can learn how to solve these problems myself.
Stream.of("A","B","C")for creating a three element stream ad hoc. - Holger