基于映射迭代的Scala理解
我已经阅读了大量的代码和教程,但我仍然不明白如何通过迭代来理解地图 例如:我有一张地图。键作为字符串(学生名称),值作为学生详细信息。我想迭代学生地图作为每个键和值。我如何通过理解来实现这一点 这是我试过的代码,但我没能理解基于映射迭代的Scala理解,scala,yield,flatmap,for-comprehension,Scala,Yield,Flatmap,For Comprehension,我已经阅读了大量的代码和教程,但我仍然不明白如何通过迭代来理解地图 例如:我有一张地图。键作为字符串(学生名称),值作为学生详细信息。我想迭代学生地图作为每个键和值。我如何通过理解来实现这一点 这是我试过的代码,但我没能理解 for { studentMap <- studRepo.getAllStudent()// returns a map result1 <- performSomeOper(studentMap.key) // I’m not getting a
for {
studentMap <- studRepo.getAllStudent()// returns a map
result1 <- performSomeOper(studentMap.key) // I’m not getting an option to access the key/value
result2 <- performSomeOper(studentMap.value)
} yield performYieldOps(result1, result2)
用于{
studentMap很棒。这太棒了。如果我有值as List,我如何从该列表中获取第一项?我需要执行foreach还是有方法访问like value。获取(0)?顺便说一句,您保存了我的日期list.headOption
或list.head
我收到了编译器错误,因为该键应该是字符串,但它给出了Any类型,它给出了一系列错误,这些错误都与类型不匹配有关。
for {
(key, value) <- studRepo.getAllStudent()
res1 <- performSomeOper(key)
res2 <- performSomeOper(value)
} yield ...
m.flatMap {
case (key, value) => ...
}