简体   繁体   English

Kotlin:如何将列表中的字段 map 到新列表并将其分组

[英]Kotlin: How to map a field from list to a new list and group it

I have two lists我有两个清单

list1 = [ { city: 'Los Angeles', population: '4 million ' } ... ]
list2 = [ { nameOfCity: 'New York', language: 'English' }, {nameOfCity: 'Berlin', language: 'German' ]

Now I need to create a new list and filter the two lists by the name of city and then group it by the same language现在我需要创建一个新列表并按城市名称过滤这两个列表,然后按相同的语言对其进行分组

newClass(val nameOfCities: List<String>, language: String)

So I need to check if city names of list2 are inside list 2 (list2.filter { it.nameOfCity.= list1.city } and store all the names inside the nameOfCities list grouped by language所以我需要检查 list2 的城市名称是否在列表 2 中(list2.filter { it.nameOfCity.= list1.city } 并将所有名称存储在按语言分组的 nameOfCities 列表中

so the end result should be something like this:所以最终结果应该是这样的:

[
{ nameOfCities: [New York, Chicago]
  language: Engilsh
},
{ nameOfCities: [Berlin]
  language: German
 }
]

So I get a new list of all the cities that don't exists in list1 and grouped by the same language.因此,我得到了 list1 中不存在并按相同语言分组的所有城市的新列表。

This is the closest I got:这是我得到的最接近的:

    for (item in list1) {
            reducedList = (list2.filter { it.nameOfCity != item.city }.map { newClass(it.nameOfCity, it.language) })
                .toList().groupBy { it.language }
        }

But the format is like this:但是格式是这样的:

'english': [
    { nameOfCity: 'Los Angeles', language: 'English' },
    { nameOfCity: 'New York', language: 'English' },
],
'german': [ { nameOfCity: 'Berlin ', language: 'German' } ]

I'm not sure I understood what you meant but i try to help you:我不确定我是否理解您的意思,但我会尽力帮助您:

I am assuming that you have 3 classes like as follow:我假设您有 3 个类,如下所示:

class newClass(val nameOfCities: List<String>, val language: String)
class ListElement1(val city: String, val population: String)
class ListElement2(val nameOfCity: String, val language: String)

And 2 lists:和 2 个列表:

val list1 = listOf(ListElement1("Los Angeles", "4 million"))
val list2 = listOf(ListElement2("New York", "English"), ListElement2("Berlin", "German"))

Filter list2 in order to delete all cities that are in list1 and map all to a list of newClass:过滤 list2 以便将 list1 和 map 中的所有城市全部删除到 newClass 列表中:

var reducedList: List<NewClass> = listOf()
list1.forEach{ listElement1 ->
    = list2.filter { listElement1.city != it.nameOfCity }
           .groupBy { it.language }.map { newClass(it.value.map { it.nameOfCity }, it.key) }
    }

Result(json):结果(json):

[
  {
    language = "English",
    nameOfCities= [ 
      "New York"
    ]
  },
  {
    language = "German",
    nameOfCities= [
      "Berlin"
    ]
  }
]

Result(Classes):结果(类):

[
  newClass(nameOfCities=[New York], language=English), 
  newClass(nameOfCities=[Berlin], language=German)
]

Maybe this one will be helpfull:也许这个会有所帮助:

        list2.filter { list2Element -> !list1.any { it.city == list2Element.nameOfCity} }
        .groupBy({it.language}, {it.nameOfCity}).map { newClass(it.value, it.key) }

Instead of iterating and filtering you can simply use.any()您可以简单地使用.any() 而不是迭代和过滤

I don't know if I understood your question right if not I am sorry I am still new to this.我不知道我是否正确理解了您的问题,如果不是,我很抱歉我还是新手。 I would maybe use the.contains() method in the List interface(whatever List you're using works, ArrayList, LinkedList, and Vector I am pretty sure they all have that method cause they implement the List interface).我可能会在 List 接口中使用 .contains() 方法(无论您使用的 List 是否有效,ArrayList、LinkedList 和 Vector 我很确定它们都有该方法,因为它们实现了 List 接口)。 So I would go through one of the lists using a for each and use the.contains() method.所以我会 go 通过使用 for each 的列表之一并使用 .contains() 方法。 I hope this helped I know it's not the best answer.我希望这有助于我知道这不是最好的答案。 Sorry if this wasn't the answer you were looking for and have a nice day.抱歉,如果这不是您正在寻找的答案,祝您有愉快的一天。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM