Techiio-author
Started by Gibson DavidsonNov 3, 2021

Open
How to convert a hashmap to csv file in Kotlin

1 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWES SHARE

I'm presently developing an app in which I create a CSV report through hashmap in Kotlin. My modern hashmap looks like this:

Val savehash:mutablemap<String, MutableList> = mutablemapof<String, MutableList>()

(a1 = [124,125,125,128.....]),(b1 = [121,125,126,124,129....])

I would like to have every key be the column with each element of the list becoming the column values(rows). How could I convert this into Kotlin?

2 Replies

Techiio-commentatorBruno Fernandez replied 2 months ago0 likes0 dislikes

You can go with pure Kotlin if values in your list doesn't include valuesSeparator (comma) and lineTerminator (CRLF) symbols (in that case you'd better use some libraries, like commons-csv, which will handle escaping symbols for you):

val csv = File("path\\to\\your\\csv\\output\\file")
val valuesSeparator = ","
val lineTerminator = "\r\n"
//write header, overwrite file if it has existed
csv.writeText(savehash.keys.joinToString(separator = valuesSeparator, postfix = lineTerminator))
//write rows
val nRows =
//  savehash.values.first().size        // if it's guaranteed that size of all lists is the same
    savehash.values.maxOf { it.size }   // otherwise
for (i in 0 until nRows) {
    val row = savehash.values
        // .map { it[i] }               // if it's guaranteed that size of all lists is the same
        .map { it.getOrElse(i) { "" } } // otherwise
        .joinToString(separator = valuesSeparator, postfix = lineTerminator)
    csv.appendText(row)
}
Techiio-commentatorBruno Fernandez replied 2 months ago0 likes0 dislikes

You can go with pure Kotlin if values in your list doesn't include valuesSeparator (comma) and lineTerminator (CRLF) symbols (in that case you'd better use some libraries, like commons-csv, which will handle escaping symbols for you):

val csv = File("path\\to\\your\\csv\\output\\file")
val valuesSeparator = ","
val lineTerminator = "\r\n"
//write header, overwrite file if it has existed
csv.writeText(savehash.keys.joinToString(separator = valuesSeparator, postfix = lineTerminator))
//write rows
val nRows =
//  savehash.values.first().size        // if it's guaranteed that size of all lists is the same
    savehash.values.maxOf { it.size }   // otherwise
for (i in 0 until nRows) {
    val row = savehash.values
        // .map { it[i] }               // if it's guaranteed that size of all lists is the same
        .map { it.getOrElse(i) { "" } } // otherwise
        .joinToString(separator = valuesSeparator, postfix = lineTerminator)
    csv.appendText(row)
}
You must be Logged in to reply
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance