Tag: hashmap

如何将字符串的某些字符与Kotlin中的HashMap 的值交换?

假设我有一个 val s: String = “14ABC5” 并有一个HashMap val b: HashMap = hashMapOf(“A” to “10”, “B” to “11”, “C” to “12”, “D” to “13”, “E” to “14”, “F” to “15” ) 我将如何在保持顺序(“1”,“4”,“10”,“11”,“12”,“5”)的同时,将10,11,12中的所有A,B, 到目前为止,我有这个 val result: List = s.toUpperCase().toCharArray().map{ it.toString() }.map{ it -> b.getValue(it)} 如果String所有字符都存在于HashMap但是我的String也可能包含不存在的键,这将起作用。

通过Android意图传递HashMap

我有一个HashMap(String,HashMap(String,Object))在我的一个活动。 我如何通过意图发送这个HashMap到另一个活动 如何将这个HashMap添加到intent extras中?

在kotlin中转换地图的习惯方法?

在斯卡拉,这只是map功能。 例如,如果hashMap是一个字符串的hashMap,那么你可以执行以下操作: val result : HashMap[String,String] = hashMap.map(case(k,v) => (k -> v.toUpperCase)) 但是,在Kotlin中, map将地图变成了一个列表。 在Kotlin做同样的事情是否有一种惯用的方式?

错误:类型推断失败

我正在尝试编写一个Android Kotlin应用程序,但是我得到了错误。 我哪里错了? 这就是我已经宣布我的HashMap: var mParent: MutableMap<*, *> ?= null mParent = HashMap() 错误: 类型推断失败。 没有足够的信息来推断构造函数HashMap中的参数K. 请明确指定

如何在Kotlin的HashMap中对Arraylist的项目进行分组?

我需要把时间分组在一起,比如02:10 PM,02:30 PM应该在02:00 PM – 03:00 PM。 我以yyyy-MM-dd'T'HH:mm:ss.SSS + 00:00格式获得所有时间。 如何将时间分组在一起02:10 PM格式? 目前,我把它们转换成hh:mm aa格式,并得到了列表,如何将它们分组到Arraylists中,最后将它们存储在Map中,以便我可以在Recyclerview中列出时间?

HashMap(它)会做什么?

我正在学习有关“Android开发人员的Kotlin”示例代码https://github.com/antoniolg/Kotlin-for-Android-Developers 在代码.parseList { DayForecast(HashMap(it)) } ,我不明白什么函数HashMap(它)会做。 HashMap()是一个类,接受一个parmater吗? 而且,我认为DayForecast(…)..类的完整代码是代码A,对不对? 再次,如果我创建一个对象var myDayForecast=DayForecast(10L,"Desciption",10,5,"http://www.a.com",10L) ,myDayForecast.map将是空的,对吗? 代码A class DayForecast(var map: MutableMap<String, Any?>) { var _id: Long by map var date: Long by map var description: String by map var high: Int by map var low: Int by map var iconUrl: String by map var cityId: Long by map constructor(date: Long, […]

如何在Kotlin的嵌套Hashmap迭代中使用`filterValues`?

我正在尝试使用Kotlin以更实用的风格迭代嵌套的Hashmap。 特别是,我有两个HashMap实例(都是Map<String, Integer> ),我想根据一些条件匹配它们的值。 这是我想要实现的Java方法: Map<Integer, Integer> pairs = new LinkedHashMap<>(); for (Map.Entry<String, Integer> oldImages : startingImageMap.entrySet()) { for (Map.Entry<String, Integer> newImages : finalImageMap.entrySet()) { Integer oldImageVal = oldImages.getValue(); Integer newImageVal = newImages.getValue(); boolean containsNewType = pairs.containsKey(newImageVal) && pairs.get(newImageVal).intValue() == oldImageVal.intValue(); boolean urlsMatch = newImages.getKey().equals(oldImages.getKey()); if (!containsNewType && urlsMatch) { pairs.put(oldImageVal, newImageVal); } } } […]

在Kotlin中使用不同的值类型实现哈希表

是否有可能在Kotlin有一个HashMap,采取不同的值类型? 我试过这个: val template = "Hello {{world}} – {{count}} – {{tf}}" val context = HashMap<String, Object>() context.put("world", "John") context.put("count", 1) context.put("tf", true) …但这给了我一个类型不匹配(显然是"John" , 1和true的不是对象) 在Java中,你可以通过创建类型new String("John") , new Integer(1) , Boolean.TRUE ,我试过了Kotlin中的等价物,但仍然得到了类型不匹配的错误。 context.put("tf", Boolean(true)) 有任何想法吗?

方法hashMapOf()在Kotlin中

任何人都可以给我一个hashMapOf()方法的具体例子,我应该什么时候使用它? 如果我做这样的事情: val map2 : HashMap<String, String> = hashMapOf() map2["ok"] = "yes" 这意味着我可以使用它来初始化map2属性。 但是像Kotlin中的其他方法一样: val arr = arrayListOf<String>("1", "2", "3") 有什么办法可以像上面那样使用这个方法吗?