JAVA调用Scala编程

下面这幅图摘自快学scala

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
  }
  }

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 javamap=Counter$.MODULE$.wordCount("aaaaereaeret");
		System.out.println(javamap);
}} 
  
```
 
  

你可能感兴趣的:(Scala学习)