简体   繁体   English

如何使用Kotlin将列表转换成地图

[英]How to transform a list into a map with Kotlin

I'm trying to construct a map from a list. 我正在尝试从列表中构建地图。 My goal is to compare two lists and found differences between thoses two lists. 我的目标是比较两个列表,并发现这两个列表之间的差异。 Then, I want to construct a map, in order to know in which index I found differences. 然后,我想构造一个地图,以便知道在哪个索引中发现了差异。

I did it in Java, not in a great way I believe, but it's working. 我用Java做到了这一点,虽然我认为不是很好,但是它正在工作。

//I compare the two values for a given index, if value are the same, I set null in my result list
List<String> result = IntStream.range(0, list1.size()).boxed()
                .map(i -> list1.get(i) != list2.get(i) ? (list1.get(i)  + " != "+ list2.get(i)) : null)
                .collect(Collectors.toList());

//I filter all the null values, in order to retrieve only the differences with their index
Map<Integer, String> mapResult =
            IntStream.range(0, result.size())
            .boxed().filter(i-> null != result.get(i))
            .collect(Collectors.toMap(i -> i,result::get));

It's not optimal, but it's working. 这不是最佳选择,但是可以正常工作。 If you have suggestions regarding thoses lines of codes, I will gladly take it. 如果您对这些代码行有建议,我会很乐意接受。

I tried two replicate this kind of behavior in Kotlin, but I didn't succeed to use the map() constructor. 我尝试两次在Kotlin中复制这种行为,但是我没有成功使用map()构造函数。 (I'm still learning Kotlin, I'm not very familiar with it). (我仍在学习Kotlin,对此我不太熟悉)。

Thank you for your help. 谢谢您的帮助。

You may use zip function in collections to join two elements. 您可以在集合中使用zip函数来连接两个元素。 The withIndex() function helps to turn a list into a list of pairs of an element index and value. withIndex()函数有助于将一个列表变成元素索引和值对的列表。 The full solution may be as follows 完整的解决方案可能如下


    val list1 = listOf("a", "b", "c")
    val list2 = listOf("a", "B", "c")

    val diff : Map<Int, String> = list1.withIndex()
        .zip(list2) { (idx,a), b -> if (a != b) idx to "$a != $b" else null}
        .filterNotNull().toMap()

Note that the zip function iterates while there are elements in both lists, it will skip a possible leftover from any of the lists. 请注意,当两个列表中都有元素时, zip函数将进行迭代,它将跳过任何列表中的剩余内容。 It can be fixed by adding empty elements with the following function: 可以通过添加具有以下功能的空元素来解决此问题:


fun <T> List<T>.addNulls(element: T, toSize: Int) : List<T> {
    val elementsToAdd = (toSize - size)
    return if (elementsToAdd > 0) {
        this + List(elementsToAdd) { element }
    } else {
        this
    }
}

and call the function on both lists before using the zip function 并在使用zip函数之前在两个列表中都调用该函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 kotlin 如何对列表进行排序<Map<String, Any> &gt;? - kotlin how to sort List<Map<String, Any>>? Kotlin:如何将列表中的字段 map 到新列表并将其分组 - Kotlin: How to map a field from list to a new list and group it 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>? 如何转换列表 <Map<Customer, Map<key, BigDecimal> &gt;&gt;进入地图 <Customer, Map<key, BigDecimal> &gt; - how to transform a List<Map<Customer, Map<key, BigDecimal>>> into a Map<Customer, Map<key, BigDecimal>> 如何转换地图 <String, List<String> &gt;按键值放入文件 - How to transform Map<String, List<String>> into Files by Key Value 如何转换列表<String>到地图<String,String>使用 Google 收藏? - How to transform List<String> to Map<String,String> with Google collections? 如何转换地图 <String,String> 列表 <String> 使用谷歌收藏 - how to transform Map<String,String> to List<String> using google collections 在java中将对象列表转换为地图 - Transform List of object to a Map in java RxJava将List转换为Maps - RxJava transform List into a Map of Maps 将列表转换为地图 - foreach或stream? - Transform a list into a map - foreach or streams?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM