Java 如何遍历scala中的每个状态?

Java 如何遍历scala中的每个状态?,java,scala,testing,apiclient,Java,Scala,Testing,Apiclient,我试图遍历每个状态,以检查ArrayList是否至少有1个活动和1个非活动状态 var active = false; var inactive = false; for (item <- reasons.items) { if(item.status == "ACTIVE") active = true if(item.status == "INACTIVE") } active must be (true) inactive must be (true) 注:

我试图遍历每个状态,以检查ArrayList是否至少有1个活动和1个非活动状态

var active = false;
var inactive = false;
for (item <- reasons.items) {
  if(item.status == "ACTIVE")
    active = true
  if(item.status == "INACTIVE")
    }
active must be (true)
inactive must be (true)

注:原因保留项(共1项)。items包含可以像reasons.items.get(x)一样调用的单个项。

干净的方法是

val active = reasons.items.exists(item => item.status == "ACTIVE")
或更短

val active = reasons.items.exists(_.status == "ACTIVE")

对于
val-inactive
也类似。这确实存在迭代列表两次的问题(但在找到合适的项后立即停止,这两次与代码中的情况不同)

清洁的方法是

val active = reasons.items.exists(item => item.status == "ACTIVE")
或更短

val active = reasons.items.exists(_.status == "ACTIVE")
对于
val-inactive
也类似。这确实存在迭代列表两次的问题(但在找到合适的项后立即停止,这两次与代码中的情况不同)

对于“至少1”,您可以在
上使用
存在
,检查给定的谓词,如果至少有一个项满足条件,则返回true。对于“活动和非活动”,您可以使用&&,将两个
存在的
调用组合在一起,以实现效率低下的方法

reasons.items.exists(_.status.equals("ACTIVE")) && reasons.items.exists(_.status.equals("INACTIVE"))`
对于“至少1”,您可以在
上使用
存在
,检查给定的谓词,如果至少有一个项满足条件,则返回true。对于“活动和非活动”,您可以使用&&,将两个
存在的
调用组合在一起,以实现效率低下的方法

reasons.items.exists(_.status.equals("ACTIVE")) && reasons.items.exists(_.status.equals("INACTIVE"))`

其他答案很好地解释了如何使用Scala集合实现这一点。因为看起来您使用的是ScalaTest,所以我想补充一点,您也可以使用ScalaTest在元素上循环

使用Inspector中的循环样式语法:

forAtLeast(1, reasons.items) { item =>
  item.status must be ("ACTIVE")
}

forAtLeast(1, reasons.items) { item =>
  item.status must be ("INACTIVE")
}

请注意,检查器是与匹配器分开定义的,因此您必须
导入org.scalatest.inspectors.\u
使用org.scalatest.inspectors扩展…才能将
foratelast
导入范围

如果要避免检查器使用循环样式语法,可以将检查器速记语法与基于反射的
have
语法一起使用:

atLeast(1, reasons.items) must have ('status "ACTIVE")
atLeast(1, reasons.items) must have ('status "INACTIVE")
如果要避免
have
的基于反射的语法,可以扩展
have
语法,直接支持
状态
属性:

def status(expectedValue: String) =
  new HavePropertyMatcher[Item, String] {
    def apply(item: Item) =
      HavePropertyMatchResult(
        item.status == expectedValue,
        "status",
        expectedValue,
        item.title
      )
  }

atLeast(1, reasons.items) must have (status "ACTIVE")
atLeast(1, reasons.items) must have (status "INACTIVE")
或者,如果您更喜欢
be
而不是
have
,则可以扩展
be
语法以添加对
活动
非活动
的支持:

class StatusMatcher(expectedValue: String) extends BeMatcher[Item] {
  def apply(left: Item) =
    MatchResult(
      left.status == expectedValue,
      left.toString + " did not have status " + expectedValue,
      left.toString + " had status " + expectedValue,
    )
}

val active = new StatusMatcher("ACTIVE")
val inactive = new statusMatcher("INACTIVE")

atLeast(1, reasons.items) must be (active)
atLeast(1, reasons.items) must be (inactive)

在这里的示例中,定义自己的匹配器只是为了在一个断言中保存几个单词似乎有点愚蠢,但是如果您针对相同的属性编写数百个测试,那么将断言简化为一行并仍然可以自然地读取就非常方便了。根据我的经验,如果在很多测试中重用匹配器,那么像这样定义自己的匹配器是有意义的

其他答案很好地解释了如何使用Scala集合实现这一点。因为看起来您使用的是ScalaTest,所以我想补充一点,您也可以使用ScalaTest在元素上循环

使用Inspector中的循环样式语法:

forAtLeast(1, reasons.items) { item =>
  item.status must be ("ACTIVE")
}

forAtLeast(1, reasons.items) { item =>
  item.status must be ("INACTIVE")
}

请注意,检查器是与匹配器分开定义的,因此您必须
导入org.scalatest.inspectors.\u
使用org.scalatest.inspectors扩展…才能将
foratelast
导入范围

如果要避免检查器使用循环样式语法,可以将检查器速记语法与基于反射的
have
语法一起使用:

atLeast(1, reasons.items) must have ('status "ACTIVE")
atLeast(1, reasons.items) must have ('status "INACTIVE")
如果要避免
have
的基于反射的语法,可以扩展
have
语法,直接支持
状态
属性:

def status(expectedValue: String) =
  new HavePropertyMatcher[Item, String] {
    def apply(item: Item) =
      HavePropertyMatchResult(
        item.status == expectedValue,
        "status",
        expectedValue,
        item.title
      )
  }

atLeast(1, reasons.items) must have (status "ACTIVE")
atLeast(1, reasons.items) must have (status "INACTIVE")
或者,如果您更喜欢
be
而不是
have
,则可以扩展
be
语法以添加对
活动
非活动
的支持:

class StatusMatcher(expectedValue: String) extends BeMatcher[Item] {
  def apply(left: Item) =
    MatchResult(
      left.status == expectedValue,
      left.toString + " did not have status " + expectedValue,
      left.toString + " had status " + expectedValue,
    )
}

val active = new StatusMatcher("ACTIVE")
val inactive = new statusMatcher("INACTIVE")

atLeast(1, reasons.items) must be (active)
atLeast(1, reasons.items) must be (inactive)

在这里的示例中,定义自己的匹配器只是为了在一个断言中保存几个单词似乎有点愚蠢,但是如果您针对相同的属性编写数百个测试,那么将断言简化为一行并仍然可以自然地读取就非常方便了。根据我的经验,如果在很多测试中重用匹配器,那么像这样定义自己的匹配器是有意义的

我喜欢你的答案,但我不相信我们正在使用ScalaTest,所以我不能这样做。forAtLeast。对于forAtLeast,需要导入哪些scalatest包?
import org.scalatest.Inspectors.\u
类MySpec扩展。。。使用org.scalatest.Inspectors
应该可以工作,请参阅。我也在答案中添加了这个。我喜欢你的答案,但我不相信我们正在使用ScalaTest,所以我不能这么做。forAtLeast。对于forAtLeast,需要导入哪些scalatest包?
import org.scalatest.Inspectors.\u
类MySpec扩展。。。使用org.scalatest.Inspectors
应该可以工作,请参阅。我也在答案中添加了这一点。