我试图找到地图和数组中的常见字符串以输出相应的值 s(来自地图,这里的值是Map [key - > value ])在Scala中,我试图不使用任何循环。示例:
输入:
Array("Ash","Garcia","Mac") Map("Ash" -> 5, "Mac" -> 4, "Lucas" -> 3)
输出:
Array(5,4)
输出是一个包含5和4的数组,因为Ash和Mac在两种数据结构中都很常见
答案 0 :(得分:3)
什么构成循环?
def common(arr: Array[String], m: Map[String,Int]): Array[Int] =
arr flatMap m.get
用法:
common(Array("Ash","Garcia","Mac")
,Map("Ash" -> 5, "Mac" -> 4, "Lucas" -> 3))
// res0: Array[Int] = Array(5, 4)
答案 1 :(得分:3)
我认为这是最优雅的解决方案,但如果数组中有重复项,结果可能不符合您的要求。
yourArray.collect(yourMap) // Array(5,4)
答案 2 :(得分:1)
scala优雅语法非常简单:
val a = Array("Ash","Garcia","Mac")
val m = Map("Ash" -> 5, "Mac" -> 4, "Lucas" -> 3)
println(m.filter { case (k, v) => a.contains(k)}.map { case (k, v) => v}.toArray)
以下是解决方案!
答案 3 :(得分:1)
使用.filter
仅查找匹配的条目,然后获取已过滤地图的值。
<强>鉴于强>
scala> val names = Array("Ash","Garcia","Mac")
names: Array[String] = Array(Ash, Garcia, Mac)
scala> val nameToNumber = Map("Ash" -> 5, "Mac" -> 4, "Lucas" -> 3)
nameToNumber: scala.collection.immutable.Map[String,Int] = Map(Ash -> 5, Mac -> 4, Lucas -> 3)
<强> .filter.map
强>
scala> nameToNumber.filter(x => names.contains(x._1)).map(_._2)
res3: scala.collection.immutable.Iterable[Int] = List(5, 4)
scala> nameToNumber.collect{case kv if names.contains(kv._1) => kv._2}
res6: scala.collection.immutable.Iterable[Int] = List(5, 4)
此处的复杂性为O(n2)
答案 4 :(得分:0)
window.addEventListener('scroll', () => {
if(this.pageYOffset < document.getElementById('firstRow').offsetTop){
document.getElementById("aboutbutton").style.backgroundColor = 'gray';
document.getElementById("archivebutton").style.backgroundColor = '#333';
document.getElementById("projectbutton").style.backgroundColor = '#333';
document.getElementById("contactbutton").style.backgroundColor = '#333';
}
if(document.getElementById('firstRow').offsetTop < this.pageYOffset && this.pageYOffset < document.getElementById('secondRow').offsetTop){
document.getElementById("aboutbutton").style.backgroundColor = '#333';
document.getElementById("archivebutton").style.backgroundColor = 'gray';
document.getElementById("projectbutton").style.backgroundColor = '#333';
document.getElementById("contactbutton").style.backgroundColor = '#333';
}
if(document.getElementById('secondRow').offsetTop < this.pageYOffset && this.pageYOffset < document.getElementById('thirdRow').offsetTop){
document.getElementById("aboutbutton").style.backgroundColor = '#333';
document.getElementById("archivebutton").style.backgroundColor = '#333';
document.getElementById("projectbutton").style.backgroundColor = 'gray';
document.getElementById("contactbutton").style.backgroundColor = '#333';
}
if(document.getElementById('thirdRow').offsetTop < this.pageYOffset && this.pageYOffset < document.getElementById('fourthRow').offsetTop){
document.getElementById("aboutbutton").style.backgroundColor = '#333';
document.getElementById("archivebutton").style.backgroundColor = '#333';
document.getElementById("projectbutton").style.backgroundColor = '#333';
document.getElementById("contactbutton").style.backgroundColor = 'gray';
}
})