有没有一种方法可以初始化SortedMap< Integer,String>.与Java中已经包含的值? [英] Is there a way to initialize a SortedMap<Integer, String> with values already included in Java?

查看:54
本文介绍了有没有一种方法可以初始化SortedMap< Integer,String>.与Java中已经包含的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似的事情

SortedMap<Integer, String> stuff = new TreeMap<Integer, String>({1:"a",2:"b"});

就像您在python中一样,但是在Java中是可能的,还是两次调用.put()的唯一方法?

much like you would do in python but is that possible in Java, or is the only way to call .put() twice?

推荐答案

从Java 9开始,您可以这样做:

Starting Java 9, you could do:

SortedMap<Integer, String> stuff = new TreeMap<>(Map.of(1, "a", 2, "b"));

Javadoc链接:

Javadoc links:

查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆