0

I need to create a HashMap of directory-to-file in scala while I list all files in the directory. How can I achieve this in scala?

val directoryToFile = awsClient.listFiles(uploadPath).collect {
  case path if !path.endsWith("/") => {
    path match {
      // do some regex matching to get directory & file names
      case regex(dir, date) => {
          // NEED TO CREATE A HASH MAP OF dir -> date. How???
      }
      case _ => None
    }
  }
}

The method listFiles(path: String) returns a Seq[String] of absolute path of all files in the path passed as argument to the function

Darth.Vader
  • 5,079
  • 7
  • 50
  • 90

3 Answers3

0

You can filter and then foldLeft:

val l = List("""/opt/file1.txt""", """/opt/file2.txt""")
val finalMap = l
                .filter(!_.endsWith("/"))
                .foldLeft(Map.empty[String, LocalDateTime])((map, s) =>
  s match {
    case regex(dir, date) => map + (dir -> date)
    case _ => map
  }
)
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
0

Try to write more idiomatic Scala. Something like this:

val directoryToFile = (for {
    path <- awsClient.listFiles(uploadPath)
    if !path.endsWith("/")
    regex(dir, date) <- regex.findFirstIn(path)
} yield dir -> date).sortBy(_._2).toMap
Rok Kralj
  • 46,826
  • 10
  • 71
  • 80
  • What `sortBy` is for? `Map` in Scala is unordered. – Victor Moroz Nov 06 '16 at 16:45
  • You said you wanted only the last (most recent) date associated with each dir. This is the purpose of the sortBy statement. You can also remove that, if you do not need it. – Rok Kralj Nov 06 '16 at 16:56
-1

You can try something like this:

val regex =  """(\d)-(\d)""".r
val paths = List("1-2", "3-4", "555")

for {

  // Hint to Scala to produce specific type
  _ <- Map("" -> "")

  // Not sure why your !path.endsWith("/") is not part of regex
  path@regex(a, b) <- paths
  if path.startsWith("1")

} yield (a, b)

//> scala.collection.immutable.Map[String,String] = Map(1 -> 2)

Slightly more complicated if you need max:

val regex =  """(\d)-(\d)""".r
val paths = List("1-2", "3-4", "555", "1-3")

for {
  (_, ps) <-
    ( for {
        path@regex(a, b) <- paths
        if path.startsWith("1")
      } yield (a, b)
    ).groupBy(_._1)
} yield ps.maxBy(_._2)

//> scala.collection.immutable.Map[String,String] = Map(1 -> 3)
Victor Moroz
  • 9,167
  • 1
  • 19
  • 23