score:2
Accepted answer
Try changing the return type of getFileNames
toList[String]
and use map(_.getName)
like so
def getFileNames(path: String): List[String] = {
val d = new File(path)
if (d.exists && d.isDirectory) {
d
.listFiles // create list of File
.filter(_.isFile)
.toList
.sortBy(_.getAbsolutePath().replaceAll("[^a-zA-Z0-9]",""))
.map(_.getName)
} else {
Nil // return empty list
}
}
Make sure .map(_.getName)
is the last in the chain, that is, after sortBy
.
better-files would simplify this to
import better.files._
import better.files.Dsl._
val file = file"."
ls(file).toList.filter(_.isRegularFile).map(_.name)
score:1
you can use getName method
and as Tomasz pointed out, filter and map can be combined to collect as following
def getFileNames(path: String): List[String] = {
val d = new File(path)
if (d.exists && d.isDirectory) {
d
.listFiles // create list of File
.collect{ case f if f.isFile => f.getName }// gets the name of the file <--
.toList
.sortBy(_.getAbsolutePath().replaceAll("[^a-zA-Z0-9]",""))
} else {
Nil // return empty list
}
}
Source: stackoverflow.com
Related Query
- create list of String from file names in provided directory
- Scala Spark : How to create a RDD from a list of string and convert to DataFrame
- Create a temporary file from a base64 string with rapture-io
- Create Json object from Circe where the value can be String or a List
- Create object instance from list of string type arguments
- Spark - create list of words from text file and the word that comes immediately after it
- create pyspark dataframes with names from list
- How to read list of string from JSON file using Scala
- Loading a directory from resource file and loading files into a list
- How do I create a seq of string from a file that is opened with managed?
- How to create a List from a HTTP response string based on certain conditions?
- How to check gaps in a String sequence derived from file names
- Get field names list from case class
- How to create DataFrame from Scala's List of Iterables?
- How to create a scala.collection.immutable.Seq from a Java List in Java?
- Create an immutable list from a java.lang.Iterator
- How to create a DataFrame from a text file in Spark
- String concatenation from within a single list
- How to create List from Range
- Create array of literals and columns from List of Strings in Spark
- Dropping multiple columns from Spark dataframe by Iterating through the columns from a Scala List of Column names
- Create a HashMap in Scala from a list of objects without looping
- How to create a Row from a List or Array in Spark using Scala
- Add column names to data read from csv file without column names
- dynamically changing library dependencies in sbt build file from provided etc
- How to create function which would make flat List from arbitrarily deeply nested lists?
- Create one hot encoded vector from category list in Spark
- Create Map from Option of List
- How to create TimestampType column in spark from string
- How to create a bigram from a text file with frequency count in Spark/Scala?
More Query from same tag
- Scala Play Framework DB Shutdown Lifecycle Hook Order
- Akka actoreRef with macWire DI, actoreRef is not set
- Scala multi-module project?
- For an object in scala to be immutable what is required?
- Why am I getting an error in simple "get" using Hibernate?
- Scala Multiple Future wrapped in Try
- Parse and Show the data of a JSON file in Scala | Meaning of .config("spark.some.config.option", "some-value").getOrCreate()
- How do make a `CustomExecutionContext` available for dependency injection in a Play 2.6 controller?
- Functor for scala.collection.Map[Int, T]
- How to find intersection between a specific node and its neighbors in Spark GraphX with Scala
- How can I normalize a matrix in spark?
- Typesafe Joins with Spark Datasets Less Safe Than I'd Expect
- JSON deserialization using reflection
- Higher order filter function
- How does one provide an unmarshaller for a Scala trait in Akka HTTP?
- Find out which field matched term in custom score script
- Family Polymorphism in Scala
- Implicits, pimpl pattern etc
- transform a feature of a spark groupedBy DataFrame
- Understanding mutable Seq
- Per project sbt repositories/config
- Why do implicit value classes have an extra method invocation?
- Can I compile a Scala project with mixed java and scala code with dependencies both ways in Maven?
- Setting up Intellij IDEA for Scala
- Library import issue in Intelij Idea and Play Framework
- Dynamically change (@main) name, in Play Framework 2.2.2 (Scala)
- F-Bound Polymorphism with Abstract Types instead of Parameter Types?
- Override spark's libraries in spark submit
- scala: how to avoid mutation?
- Scala pipe patternmatching as a variable