如何将Java 8中的map、filter和streams转换为Scala?

7

我是一名对scala非常陌生的新手,试图通过将等价的java代码转换成scala来加深理解。

如何将Java 8中的map、filter和stream转换成Scala?

以下是我正在尝试转换为Scala的Java 8代码:

 public Set<String> getValidUsages(String itemId, long sNo, Date timeOfAccess) {                                                     
     Set<String> itemSet = Sets.newHashSet();                                                                                                   
     TestWindows testWindows = items.get(itemId).getTestWindows();                                                          

     final boolean isTV = existsEligibleTestWindow(testWindows.getTV(), timeOfAccess);                                       
     if (isTV) {                                                                                                                              
         itemSet.add(TV);                                                                                                              
     } else {                                                                                                                                    
         final boolean isCableUseable = existsEligibleTestWindow(testWindows.getCableUse(), timeOfAccess);                         
         final boolean isWifi = existsEligibleTestWindow(testWindows.getWifi(), timeOfAccess);                               
         if (isCableUseable || isWifi) {                                                                                                     
             itemSet.add(MOVIE);                                                                                                           
         }                                                                                                                                       
     }                                                                                                                                           

     if (testWindows.getUsageIds() != null) {                                                                                           
         itemSet.addAll(testWindows.getUsageIds()                                                                                      
                 .entrySet()                                                                                                                     
                 .stream()                                                                                                                       
                 .filter(entry -> existsEligibleTestWindow(entry.getValue(), timeOfAccess))                                               
                 .map(Map.Entry::getKey)                                                                                                         
                 .collect(Collectors.toSet()));                                                                                                  
     }                                                                                                                                           

     return itemSet;                                                                                                                            
 }                                                                                                                                               

 private boolean existsEligibleTestWindow(List<TestWindow> windows, Date timeOfAccess) {                                           
     if (windows != null) {                                                                                                                      
         return windows.stream()                                                                                                                 
                 .filter(w -> withinDateRange(timeOfAccess, w))                                                                                  
                 .findAny()                                                                                                                      
                 .isPresent();                                                                                                                   
     }                                                                                                                                           
     return false;                                                                                                                               
 }                                                                                                                                               

 private boolean withinDateRange(Date toCheck, TestWindow window) {                                                                       
     return toCheck.after(window.getStartTime()) && toCheck.before(window.getEndTime());                                                         
 }  

我尝试过:

 def withinDateRange(toCheck: Date, window: TestWindow): Boolean = {
    toCheck.after( window.getStartTime ) && toCheck.before( window.getEndTime )
  }


  def getValidUsages(itemId: String, sNo: Long, timeOfAccess: Date): Set[String] = {
    var itemSet = Sets.newHashSet()
    val testWindows = items.value(itemId).getTestWindows
    val isTV = existsEligibleTestWindow(testWindows.get(0).getTV, timeOfAccess)
    if (isTV) {
      itemSet += TV
    } else {
      val isCableUseable = existsEligibleTestWindow(testWindows.get(0).getCableUse, timeOfAccess)
      val isWifi = existsEligibleTestWindow(testWindows.get(0).getWifi, timeOfAccess)
      if (isCableUseable || isWifi) {
        itemSet += MOVIE
      }
    }
    if (testWindows.get(0).getUsageIds != null) {
      itemSet.addAll(testWindows.get(0).getUsageIds.entrySet().stream()
        .filter((x) => existsEligibleTestWindow(x._2, timeOfAccess)).map(x => Map.Entry._1 )
        .collect(Collectors.toSet()))
    }
    itemSet
  }


   def existsEligibleConsumptionWindow(windows: List[ConsumptionWindow], timeOfAccess: Date): Boolean = {
if (windows != null) {
  return windows.exists((x) => withinDateRange(timeOfAccess, x))
}
false
}

但是在进行过滤和流处理时出现了错误。有人能指出具体方向吗?有什么参考资料吗?我在getValidUsages上发生了错误:
 compile error “cannot resolve reference project with such signature

什么是错误信息? - Nyavro
你试过查看Javadoc吗?不需要将Java语法转换为Scala语法。直接在List、Set上使用filtermapisEmpty怎么样?不需要非惰性流。 - waltersu
2
如果您希望继续使用Java集合,您可能需要这个:https://github.com/scala/scala-java8-compat#using-java-8-streams-with-scala-function-converters - Alvaro Carrasco
@Nyavro 已更新 - Swetha
你可以使用import scala.collection.JavaConversions._,详见http://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html。 - gaston
3个回答

1
这有些难以回答,因为我对你使用的某些类型不熟悉。但如果我猜测有以下类型:
trait Window {
  def getStartTime: LocalDate
  def getEndTime: LocalDate
}
trait TestWindows extends Window {
  def getTV: List[Window]
  def getCableUse: List[Window]
  def getWifi: List[Window]
  def getUsageIds: Map[String, List[Window]]
}

那么您可以这样做:

然后您可以这样做:

  def withinDateRange(toCheck: LocalDate)(window: Window): Boolean =
    window.getStartTime.isBefore(toCheck) && window.getEndTime.isAfter(toCheck)

  // Nothing should ever be null in Scala. If it's possible you don't have any ConsumptionWindows you should either
  // model it as an empty list or an Option[List[ConsumptionWindow]]
  def existsEligibleTestWindow(windows: List[Window],
                               timeOfAccess: LocalDate): Boolean =
  windows.exists(withinDateRange(timeOfAccess))


  def getValidUsages(testWindows: TestWindows, timeOfAccess: LocalDate): Set[String] = {
    val isTV = existsEligibleTestWindow(testWindows.getTV, timeOfAccess)
    val isCableUse = existsEligibleTestWindow(testWindows.getCableUse, timeOfAccess)
    val isWifi = existsEligibleTestWindow(testWindows.getWifi, timeOfAccess)
    val tvOrMovie: Option[String] = if (isTV) Some("TV")
                                    else if (isCableUse || isWifi) Some("MOVIE")
                                    else None

    val byUsageId = testWindows.getUsageIds.collect { case (key, windows) if existsEligibleTestWindow(windows, timeOfAccess) => key }.toSet

    tvOrMovie.toSet ++ byUsageId
  }

在你的原始代码中,可能有一些items值,但在上面的代码中,我假设你在getValidUsages函数之外执行了TestWindows testWindows = items.get(itemId).getTestWindows()
我的示例根本不使用Java结构,而只使用Scala核心集合。另一个主要区别是我使用不可变数据结构,这使得代码更易于理解,通常也更安全。
需要注意的一些事项: 1)当在None上调用Option.toSet操作时,结果将为空集。 2)withinDateRange方法使用了函数柯里化的示例。 3)显然,我不知道你的原始类型是什么,必须猜测相关部分。

0
问题似乎在于你在Scala中使用了Java类型,同时依赖于Scala的map和filter操作。这会带来一些麻烦,但如果你首先将列表/集合转换为Scala的集合(警告,Scala类型默认是不可变的),那么你应该能够直接使用map/filter操作,而无需调用Java的stream()方法。

-1
def getValidUsages(itemId: String, sNo: long, timeOfAcess: Date): Set[String] = {
  var itemSet: Set[String] = Sets.newHashSet()
  val testWindows: TestWindows = items.get(itemId).getTestWindows()
  val isTV: Boolean  = existsEligibleTestWindow(testWindows.getTV(), timeOfAccess)

  isTV match {
    case true => itemSet.add(TV)
    case false => {
      val isCableUseable: Boolean = existsEligibleTestWindow(testWindows.getCableUse(), timeOfAcess)
      val isWifi: Boolean = existsEligibleTestWindow(testWindows.getWifi(), timeOfAccess)
      if(isCableUseable || isWifi) {
        itemSet.add(MOVIE)
      }
    }
  }

  if(testWindows.getUsageIds() != null) {
    itemSet.addAll(testWindows.getUsageIds()
                .stream.
                .filter(entry => existsEligibleTestWindow(entry._2, timeOfAccess))
                .map(filteredData => Map.Entry._1)
                .collect Collectors.toSet())
  }
  itemSet
}

def existsEligibleTestWindow(windows: List[TestWindow], timeOfAcess: Date): Boolean = {
  windows match {
    case null => false
    case _ => windows.stream.filter(data => withinDateRange(timeOfAcess), data).findAny().isPresent
  }
}

祝你好运 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接