hello I hav a MutableList of Ints, where each Int ...
# getting-started
o
hello I hav a MutableList of Ints, where each Int is like this: 24 24 25 26 25 26 24 I want this to be coverted to a map with Key being either 24 or 25 and value being the number of 24s. Like this: 24 3 25 2 26 2 Is there an easy function in the library to do that?
n
Copy code
val map = list.asSequence().toSet().associate { key -> key to list.count { it == key }}
sequence and toSet calls just to avoid going over each key multiple times, could proably be more efficient still
o
Can you explain it?
i under stand the asSequence and toSet part
key -> key to list.count { it == key <--- this part pls
n
you want to use the number itself as key and the amoun that number appears in the list as value
o
yes ofc
key to list.count does what?
n
associate
takes the key and returns a Pair,
key to value
to
is a inline function that creates a Pair, this is what the associate function turns into a map
o
ok
n
same as
mapOf(key to value, key2 to value2)
o
and list.count { it == key } checks if the it variable of the list is the key?
n
there might b a nicer function that just expects the value as return..
o
so
n
no it counts it with the condition being that it equals the key eg: assume the keyis 26, it will loop though list and count only occurences that match the predicate
o
oh
so btw, did you do Sololearn? its an app in the Android store
n
no, i learned the basics from a friend and by experimenting around.. and the docs, idea and this slack
mostly slack though, the most interesting things to learn are things you do not even know about until someone mentions them
o
ok
your name reminds me of someone on tht app
anyways thnx for the tip
n
ahh, wel li wanted to get into andoird at some poit but i never made a app, or released one
o
yeah i want to make a game too, using libgdx
do you know how to create an instance of MutableMap.MutableEntry ?
n
the logic is all fine butonce it gets into graphics programming i am not understanding anything
o
nah libgdx makes it easy to program in
n
where do you need that ?
o
it handles all the file handling and opengl graphics drawing
well,this code
Copy code
fun hasIt(dmg: Int, totalDmgMap: MutableMap<Int,Int>): Pair<Boolean,MutableMap.MutableEntry<Int,Int>> {
    var found = false
    var entry = MutableMap.MutableEntry

    for(i in totalDmgMap) {
        if(i.key == dmg) {
            found = true
            entry = i
        }
    }

    return Pair(found,entry)
}`
n
that seemslike you are misusing it somehow, usually istead of Entry oyu would be fine to use a Pair
o
oh
maybe i should use a triple
n
i was jsut about to type that
o
heh
n
and you seem to loop over all in the map and set it for every fond item, is that what you want ?
you may map somethimg like map.key.find { it.key == dmg }
o
nah im just testing my damge calculating algo, and making a 1000 times loop into a map
actually i dont need tht function
n
this will return the first it found or null
a
An even more efficient version would be
list.groupBy { it }.mapValues { it.value.count }
4
n
yep this seems lot more efficient and simpler code
i
1