JAVA调用Scala编程

司马昕
2023-12-01
下面这幅图摘自快学scala
<img src="https://img-blog.csdn.net/20150508225456572?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTUwOTQwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
Scala代码
```
package classtest

import scala.beans.BeanProperty
import scala.collection.mutable.HashMap
import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConversions._
class Counter {
def wordCount(str:String): java.util.Collection[Object]={
    println("----调用scala开始---")
    var list = new ArrayBuffer[String]
    for (i <- 0 to str.length - 1) {
      list+= (str.charAt(i).toString())
    }
    var map1 = new scala.collection.mutable.HashMap[String, Int]
    for (key <- list.toList) {
      map1(key) = map1.getOrElse(key, 0) + 1
    }
    map1
  }
  }

<pre name="code" class="java">package classtest;
/**
*java调用scala
java使用伴生对象: 伴生对象名$.MODULE$.方法名();
/
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class JavaMap {

	public static void main(String[] args) {
		
		Collection<Object> javamap=Counter$.MODULE$.wordCount("aaaaereaeret");
		System.out.println(javamap);
}}

```
 

 类似资料: