1:java的map转scala的map

//java的map转为scala的immutable.map
    private static scala.collection.immutable.Map<String,String> JavaMapToScala(Map kafkaparams) {
        Map<String, String> javamap = new HashMap<>();
        scala.collection.mutable.Map scalaMap = JavaConversions.mapAsScalaMap(javamap);
        Object objTest = Map$.MODULE$.<String,String>newBuilder().$plus$plus$eq(scalaMap.toSeq());
        Object resultTest = ((scala.collection.mutable.Builder) objTest).result();
        scala.collection.immutable.Map<String,String> scala_imMap = (scala.collection.immutable.Map)resultTest;
        return scala_imMap;
    }

2:scala的immutable和java的mutable的map转换

2.1:mutable.Map to immutable.Map

val map = mutable.Map(1 -> "zhangsan", 2 -> "lisi")
val result = map.toMap

2.2:immutable.Map to mutable.Map

val map = immutable.Map(1 -> "zhangsan", 2 -> "lisi")
val result = mutable.Map(map.toSeq: _*)

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐