I have a code like this:
public class A {
private String id;
//constructor, getter, setter
}
In java I can use this:
public class C {
@Test
public void t() {
List<A> list = Arrays.asList(new A("1"));
list.sort(Comparator.comparing(A::getId));
Map<String, List<A>> map = list.stream().collect(Collectors.groupingBy(A::getId));
}
}
Here is scala(2.12) test:
class B {
@Test
def t(): Unit = {
val list = util.Arrays.asList(new A("1"))
list.sort(Comparator.comparing(a => a.getId))
list.stream().collect(Collectors.groupingBy(a => a.getId))
}
}
But in scala test, list.sort(Comparator.comparing(a => a.getId))
will get two errors:
Error:(21, 26) inferred type arguments [com.test.A,?0] do not conform to method comparing's type parameter bounds [T,U <: Comparable[_ >: U]]
Error:(21, 38) type mismatch; found : java.util.function.Function[com.test.A,String] required: java.util.function.Function[_ >: T, _ <: U]
and list.stream().collect(Collectors.groupingBy(a => a.getId))
will get this error:
- Error:(22, 49) missing parameter type
How can I use it?
List
or Java one in Scala? – Alexey Romanov