Loop through Map in Groovy?
Quite simple with a closure: def map = [ ‘iPhone’:’iWebOS’, ‘Android’:’2.3.3′, ‘Nokia’:’Symbian’, ‘Windows’:’WM8′ ] map.each{ k, v -> println “${k}:${v}” }
Quite simple with a closure: def map = [ ‘iPhone’:’iWebOS’, ‘Android’:’2.3.3′, ‘Nokia’:’Symbian’, ‘Windows’:’WM8′ ] map.each{ k, v -> println “${k}:${v}” }
You have two choices: The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map: val map = friends.associateBy({it.facebookId}, {it.points}) The second, less performant, is to use the standard map function to create a list of Pair which can … Read more
What about /usr/share/dict/words on any Unix system? How many words are we talking about? Like OED-Unabridged?
Two terms for the same thing: “Map” is used by Java, C++ “Dictionary” is used by .Net, Python “Associative array” is used by PHP “Map” is the correct mathematical term, but it is avoided because it has a separate meaning in functional programming. Some languages use still other terms (“Object” in Javascript, “Hash” in Ruby, … Read more
Dictionaries in Swift (and other languages) are not ordered. When you iterate through the dictionary, there’s no guarantee that the order will match the initialization order. In this example, Swift processes the “Square” key before the others. You can see this by adding a print statement to the loop. 25 is the 5th element of … Read more
Go introduced a delete(map, key) function: package main func main () { var sessions = map[string] chan int{}; delete(sessions, “moo”); }
dict.fromkeys directly solves the problem: >>> dict.fromkeys([1, 2, 3, 4]) {1: None, 2: None, 3: None, 4: None} This is actually a classmethod, so it works for dict-subclasses (like collections.defaultdict) as well. The optional second argument, which defaults to None, specifies the value to use for the keys. Note that the same object will be … Read more
Edit: This has since been fixed in the latest TS versions. Quoting @Simon_Weaver’s comment on the OP’s post: Note: this has since been fixed (not sure which exact TS version). I get these errors in VS, as you would expect: Index signatures are incompatible. Type ‘{ firstName: string; }’ is not assignable to type ‘IPerson’. … Read more
This is an old question, but here’s my two cents. PeterSO’s answer is slightly more concise, but slightly less efficient. You already know how big it’s going to be so you don’t even need to use append: keys := make([]int, len(mymap)) i := 0 for k := range mymap { keys[i] = k i++ } … Read more
Answer recommended by Go Language