I’d like to read in a file and produce a `Set<S...
# getting-started
e
I’d like to read in a file and produce a
Set<String>
containing all of the words in the file. Is there a way to
map
or
flatMap
over the lines of a file? I’ve gotten as far as
bufferedReader().useLines()
.
e
@deactivateduser Yes, I could do that with
readText()
. Thank you. Unfortunately, I oversimplified my problem: I need to remove lines that, when trimmed, start with
#
. Maybe I could see how
readText()
is written and write my own version.
OK, I see that
readLines()
returns a
List<String>
. I can deal with that.
d
Ok. If your files are big though, you might want to consider another approach, as creating a List of thousands of lines could be memory consuming.
e
@deactivateduser Agreed, but the file isn’t huge.
👌 1
This seems to work. https://pl.kotl.in/RgxBdBWYM
Thanks for your help, @deactivateduser! I’m the only Kotlin programmer on my team (and remote much of the time) so I don’t have anyone local to bounce ideas off of.
d
no problem.. 👌
s
@Ellen Spertus you can even get rid of the
if-else
statement by using
filterNot
, like in this example implementation which has the same behavior: https://pl.kotl.in/nFO6cvPUU
👍 1
e
Thanks, @Sebastian Aigner!