Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert java 8 map, filter and streams to scala?

I am very new to scala trying to understand by changing the equivalent java to scala so that I get better understanding.

How to convert java 8 map, filter and streams to scala ?

I have the following java 8 code which I am trying to convert to Scala :

 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());                                                         
 }  

I tried :

 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
}

But getting error while doing filter and stream. Can some one point to direct direction ? Any references ? I am getting error on getValidUsages :

 compile error “cannot resolve reference project with such signature
like image 807
Swetha Avatar asked Sep 13 '16 00:09

Swetha


People also ask

Can we use filter and map together Java 8?

Once we got the Stream of Integer, we can apply maths to find out even numbers, and we passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2 then we would have called filter before map. That's all about how to use map and filter in Java 8.

Can we use filter and map in stream Java?

All you need is a mapping function to convert one object to the other. Then, the map() function will do the transformation for you. It is also an intermediate Stream operation which means you can call other Stream methods, like a filter, or collect on this to create a chain of transformations.

Can we use map stream in Java 8?

Java 8 Stream's map method is intermediate operation and consumes single element forom input Stream and produces single element to output Stream. It simply used to convert Stream of one type to another. Let's see method signature of Stream's map method.

Can we convert map to stream?

Converting complete Map<Key, Value> into Stream: This can be done with the help of Map. entrySet() method which returns a Set view of the mappings contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set. stream() method.


1 Answers

This is somewhat difficult to answer since I am unfamiliar with some of the types you use. But if I guess there are types like the following:

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]]
}

then you could just do this:

  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
  }

In your original code there was presumably some items value, but in the above I just assume you do TestWindows testWindows = items.get(itemId).getTestWindows() outside the getValidUsages function.

My example doesn't use java structures at all and just uses the scala core collections. The other main difference is that I use immutable data structures which is, I think, a little easier to follow and generally safer.

Some items of note: 1) The Option.toSet operation result in an empty set when called upon a None. 2) There is an example of function currying used for the withinDateRange method. 3) I obviously have no idea what your original types do and had to guess at the relevant parts.

like image 154
Idan Waisman Avatar answered Oct 03 '22 21:10

Idan Waisman