java.util.Map转化成scala.collection.immutable.Map

1.适用于jdk1.7+版本

public static scala.collection.immutable.Map toScalaImmutableMap(java.util.Map javaMap) {
        final java.util.List> list = new java.util.ArrayList<>(javaMap.size());
        for (final java.util.Map.Entry entry : javaMap.entrySet()) {
            list.add(scala.Tuple2.apply(entry.getKey(), entry.getValue()));
        }
        final scala.collection.Seq> seq = scala.collection.JavaConverters.asScalaBufferConverter(list).asScala().toSeq();
        return (scala.collection.immutable.Map) scala.collection.immutable.Map$.MODULE$.apply(seq);
    }

2.适用于jdk1.8+版本

public static  scala.collection.immutable.Map toScalaImmutableMap(java.util.Map jmap) {
        List> tuples = jmap.entrySet()
                .stream()
                .map(e -> Tuple2.apply(e.getKey(), e.getValue()))
                .collect(Collectors.toList());

        Seq> scalaSeq = scala.collection.JavaConverters.asScalaBufferConverter(tuples).asScala().toSeq()

        return (Map) scala.collection.immutable.Map$.MODULE$.apply(scalaSeq);
    }

推荐一个公众号

号主为一线大厂架构师,CSDN博客专家,博客访问量突破一千万。主要分享Java、golang架构,源码,分布式,高并发等技术,用大厂程序员的视角来探讨技术进阶、面试指南、职业规划等。15W技术人的选择!

你可能感兴趣的:(java,scala)