【问题标题】:How to check whether element exsits or not in a List[Any] using Scala如何使用Scala检查List[Any]中是否存在元素
【发布时间】:2020-07-19 08:58:41
【问题描述】:

我有一个如下列表:

val map_temp =
  List(
    (List("SRC_123","SRC_999"),"TRGT_123"),
    (List("SRC_456"),"TRGT_456"),
    (List("SRC_789"),"TRGT_789"),
    (List("SRC_888"),"TRGT_888"),
  )

我想检查SRC_999是否存在。

我尝试了以下方法:

map_temp.foreach {
  case(key, value) =>
    if (key.contains("SRC_999")) {
      print("yes exists")
    }
}

这会导致以下错误

error: value contains is not a member of Any
case(key:Any, value) => if(key.contains("SRC_99"))

我也试过这个:

map_temp.foreach(a => if(a._1.contains("SRC_999")))

但这会导致以下错误:

error: value contains is not a member of Any
mapped.foreach(a => print(a._1.contains("SRC_999")))

我该如何解决这个问题?

【问题讨论】:

    标签: list scala contains


    【解决方案1】:

    我该如何解决这个问题?

    没有List[Any]

    通过List[Any],您明确告诉Scala 元素可以是任何东西。这意味着 Scala 对元素一无所知。它甚至不知道他们是否有contains 方法。元素可以是整数,例如,没有contains 方法。

    【讨论】:

    • 我也试过这个 map_temp.map(_.asInstanceOf[(String, String)])。
    • map_temp.foreach(a => if (a._1.contains("SRC_999")){print("yes Exits" )}) 结果:java.lang.ClassCastException:scala.collection。 immutable.$colon$colon 不能转换为 java.lang.String
    【解决方案2】:

    map_temp 的值是 List[(List[String], String)],而不是 List[Any]。您可以像这样检查您要查找的元素:

    map_temp.exists(_._1.contains("SRC_999"))
    

    【讨论】:

      【解决方案3】:

      这应该做你想做的:

      val map_temp =
        List(
          (List("SRC_123","SRC_999"),"TRGT_123"),
          (List("SRC_456"),"TRGT_456"),
          (List("SRC_789"),"TRGT_789"),
          (List("SRC_888"),"TRGT_888"),
        )
      
      def exists[A](input: List[(List[A], _)], what: A): Boolean =
        input.exists(_._1.contains(what))
      
      // should be found
      assert(exists(map_temp, "SRC_999"))
      assert(exists(map_temp, "SRC_888"))
      
      // should not be found
      assert(!exists(map_temp, "SRC_998"))
      assert(!exists(map_temp, "TRGT_123"))
      

      我认为您实际上没有 List[Any] 开头。您似乎遇到了一些类型错误,这些错误可能会误导您相信这一点。您的map_tempList[(List[String], String)]

      如果不是这种情况,并且您从一个由于某种原因返回 List[Any] 的方法获得 map_temp,如果您控制该方法,则可以更改它以反映实际类型。如果你不能,即使它不安全,你也可以尝试施放它。

      只需少量更改,您还可以检索项目:

      def find[A, B](input: List[(List[A], B)], what: A): Option[B] =
        input.find(_._1.contains(what)).map(_._2)
      
      // should be found
      assert(find(map_temp, "SRC_999") == Some("TRGT_123"))
      assert(find(map_temp, "SRC_888") == Some("TRGT_888"))
      
      // should not be found
      assert(find(map_temp, "SRC_998") == None)
      assert(find(map_temp, "TRGT_123") == None)
      

      您可以使用此代码here on Scastie

      【讨论】:

      • 您好,stefanobaghino,
      • 我得到了 List[Any] ,因为如下所示。 var r = List.range(0,inst.charMapping.length) var srcChars1 = r.map{i => if(1==1) inst.charMapping(i).srcChars.get else I} var tgtChar1 = r. map{i => if(1==1) inst.charMapping(i).tgtChar.get else i} map_temp = srcChars1 zip tgtChar1
      • 如何获取 List[(List[String], String)]?
      • 所以,我假设if 条件中的1==1 是其他东西的占位符,因为它们当然总是评估为true。如果您可以删除它们,我建议您这样做。如果不能,请考虑if 表达式返回的类型必须是两个分支的超类型。例如,if (true) 4 else "4" 的类型是最接近Int 类型的4String 类型的"4" 的公共超类型,恰好是Any。要么删除if,要么确保两个分支都为srcChars1返回一个List[String],为tgtChar1返回一个String
      • 对于你写的函数,如果SRC_999退出,有没有办法从List[(List[String], String)]中获取唯一的字符串值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2019-10-02
      • 2020-06-10
      • 2011-08-07
      • 1970-01-01
      • 2011-07-14
      相关资源
      最近更新 更多