logo
  • 教程列表
  • SO官方文檔
  • 收集運算子
    • 迭代一個集合
    • 使用 collect 建立一個新列表
    • 使用 findAll 過濾列表
    • 找到匹配條件的第一個元素
    • 使用 collectEntries 建立地圖
    • 將轉換應用於巢狀集合
    • 展平巢狀列表
    • 刪除重複項
    • 從兩個列表構建一個對映
  1. StackOverflow 文件
  2. groovy 教程
  3. 收集運算子
  4. 從兩個列表構建一個對映

從兩個列表構建一個對映

Created: November-22, 2018

nrs = [1, 2, 3, 4, 5, 6, 7, 8, 9]
lets = ['a', 'b', 'c', 'd', 'e', 'f']

println GroovyCollections.transpose([nrs, lets])
        .collect {le -> [(le[0]):le[1]]}.collectEntries { it }

or
   
println [nrs,lets].transpose().collectEntries{[it[0],it[1]]}

// [1:a, 2:b, 3:c, 4:d, 5:e, 6:f]   
  • 刪除重複項

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 關於本站
  • 免責聲明