3

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:

  1. Error:(21, 26) inferred type arguments [com.test.A,?0] do not conform to method comparing's type parameter bounds [T,U <: Comparable[_ >: U]]

  2. 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:

  1. Error:(22, 49) missing parameter type

How can I use it?

xmcx
  • 283
  • 3
  • 18
  • 1
    list.sortBy( _.id).groupBy( _.id) what's wrong with this? – Raman Mishra Mar 29 '19 at 11:03
  • I only consider completing it by means of java.util.List, but how to convert to java.util.Map after groupBy. – xmcx Mar 29 '19 at 11:57
  • 1
    I hope you have heard about javaConvertors in Decorate package there you will get a method asJava so I will get converted into the java Map – Raman Mishra Mar 29 '19 at 12:25
  • Yes, I know, but I feel that it is not convenient to make multiple conversions. list -> asScala -> asJava ... – xmcx Mar 29 '19 at 13:30
  • 1. Provide exact error messages; 2. Are you using Scala 2.12? 3. Are you using Scala `List` or Java one in Scala? – Alexey Romanov Mar 29 '19 at 13:37
  • @西门吹雪 why multiple conversion? Do you want to convert the types too? If that’s the case then take java types before creating the list itself then it will be only one conversion – Raman Mishra Mar 29 '19 at 13:53
  • First of all are you using java or scala? How it can be first java then scala then again java? I mean I don’t know what you are trying to do, as well as why to first sort then create a map it makes no sense to me because the purpose of sorting will gone as soon as we do groupBy and if the list is util.List then what is the problem in conversion twice? – Raman Mishra Mar 29 '19 at 13:59
  • @Raman Mishra I am sorry for the trouble that my example has caused you, it just shows the usage of sorting and grouping. I use both languages at the same time. When I use java.util.List in scala, I don't know how to sort or group it. Now I know how to do it, but the other methods (or interfaces) receive parameters that are java type instead of scala, so I need to switch back to java type. – xmcx Mar 30 '19 at 15:05
  • @Alexey Romanov I edited my question. – xmcx Mar 30 '19 at 15:25
  • https://stackoverflow.com/questions/47380774/how-to-use-java-lambdas-in-scala – Dmytro Mitin Apr 01 '19 at 17:06
  • @Dmytro Mitin I still don't understand how to use it. Can you write a simple example? – xmcx Apr 02 '19 at 05:46
  • @西门吹雪 I added example. – Dmytro Mitin Apr 02 '19 at 09:43

2 Answers2

2

One liner should be something like this to sort the list by id and then group them by id, the map is not an ordered data structure, so the order of your id's can be in any order. What you can do is first group them and then sort Map by key which is id in this case.

case class A(id:String)
val list = List(A("1"), A("2"), A("4"), A("3"))
list.sortBy(_.id).groupBy(_.id)
Joey Ciechanowicz
  • 3,345
  • 3
  • 24
  • 48
Raman Mishra
  • 2,635
  • 2
  • 15
  • 32
2

Try

import scala.compat.java8.FunctionConverters._

val list = util.Arrays.asList(new A("1"))
list.sort(Comparator.comparing[A, String](((a: A) => a.getId).asJava))
list.stream().collect(Collectors.groupingBy[A, String](a => a.getId))

libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.9.0"

How to use Java lambdas in Scala

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66