Tag: 字典

Kotlin有一个Map语法的语法吗?

在JavaScript中: {foo: bar, biz: qux} 。 在Ruby中: {foo => bar, biz => qux} 。 在Java中: HashMap<K, V> map = new HashMap<>(); map.put(foo, bar); map.put(biz, qux); 肯定Kotlin可以比Java更好吗?

我怎么能在Kotlin有一个复合键?

在Python中,我可以有复杂的字典键,例如: d = {} d[(1, 2)] = 3 print d[(1, 2)] # prints 3 我怎样才能在Kotlin中声明和填充这样一个Map? 编辑:我试图宣布这样的地图,但我不知道如何填充它: val my_map = HashMap<Pair<Int, Int>, Int>()

Kotlin:迭代一个具有解构条目的地图是不是可能的? 错误?

看起来就像最新的Kotlin更新一样,您不能再通过具有解构Entry地图来迭代。 例如: val map = HashMap<Int, String>() for ((i, s) in map) { // compiler error here … // code } 编译器错误是For-loop range must have an iterator() method 此功能以前按预期工作,遍历地图中的每个条目。 我也尝试使用map.entries和map.entrySet() (不建议使用)和这些: 现在迭代地图的习惯用法是什么? 这是一个错误?

为什么mutableMapOf返回空值?

我写了一个简单的代码块来在Map存储带有ids fragments 。 map的value和key被初始化为非空value ,但是当我想把它们作为key和value ,它给出nullable为nullable类型数据。 所以,我不明白为什么? 有没有其他类型的地图返回Kotlin non-nullable数据?

用Map接口将元素放入HashMap

我正在尝试Kotlin,遇到了一个我无法解决的小问题。 当我有以下结构时,我可以将元素放入地图中: val map = HashMap<String, String>() map["asd"] = "s" map.put("34", "354") 然而,当我用Map接口创建一个地图时,我只能阅读它们,我在做什么错了? val map: Map<String, String> = HashMap<String, String>(); map.put("24", "34") //error map["23"] = "23" //error 或者,也许我对Kotlin的接口有些困惑?

Kotlin在Android中使用Map

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View { var view: View = inflater?.inflate(R.layout.map_fragment, null)!! var mapFragment : SupportMapFragment?=null mapFragment= fragmentManager.findFragmentById(R.id.map) as SupportMapFragment mapFragment.getMapAsync(this) return view } Logcat: FATAL EXCEPTION: main kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.gms.maps.SupportMapFragment at example.com.kotlinexamplebydimple.Mapfragment.onCreateView(Mapfragment.kt:36) 在这行上错误显示: mapFragment= fragmentManager.findFragmentById(R.id.map) as SupportMapFragment

可变的地图在Kotlin

我在我的项目中使用NamedParameterJdbcTemplate并传递参数: MapSqlParameterSource(mapOf( "userId" to userId, "count" to count )) 我不想一直写第一行,我想创建自己的函数,它将采用字符串对的值对: params( "userId" to userId, "count" to count ) 但是当我尝试实现它时,我遇到了泛型的问题(我不在这里发布错误描述): fun params(vararg pairs: Pair<String, Any>) = MapSqlParameterSource(mapOf(pairs)) 你能请正确的实施建议吗?

如何修改Kotlin的地图?

我正在试图扭转科特林的地图。 到目前为止,我已经想出了: mapOf("foo" to 42) .toList() .map { (k, v) -> v to k } .toMap() 有没有更好的方式做到这一点,而不使用中间人(中间名)?

Kotlin:收藏定义的差异

在这之间Kotlin有什么不同之处: val customerProducts = mutableMapOf<Customer, Set<Product>>() 和这个: val customerProducts: MutableMap<Customer, Set<Product>> = mutableMapOf()