Currently i'm reading a file through the Java API and adding items to a map through the method foreach which forces me to make use of the mutablemap. Is there a way of collecting the items without the mutablemap? I know there is a method collect but I couldn't get it working.
the current way:
val result = mutableMapOf<Int, MutableMap<Int, Double>>()
Files.lines(Paths.get(folderPath))
.map { line -> line.split(",") }
.map { items -> Triple(items[0].toInt(), items[1].toInt(), items[2].toDouble()) }
.forEach { (id, article, rating) ->
if (result.containsKey(id))
result[id]!!.put(article, rating)
else
result.put(id, mutableMapOf(Pair(article, rating)))
}
EDIT:
my goal is to merge the triple objects based on the first value of the triple. So a scenario would be two triple objects(1, 2, 5.5) and (1,3, 5.5). The first value of the triple is the user id, the second is the article id and the third is the rating of the article. after merging there would be one single entry in the map containing the first key = the user id, the first value of the triple, and the value would be a map containing the articles which the user rated.
It is currently working how I want it to work, but I was curious if there was a more functional way of solving this.