Iterating through a golang map

For example, package main import “fmt” func main() { type Map1 map[string]interface{} type Map2 map[string]int m := Map1{“foo”: Map2{“first”: 1}, “boo”: Map2{“second”: 2}} //m = map[foo:map[first: 1] boo: map[second: 2]] fmt.Println(“m:”, m) for k, v := range m { fmt.Println(“k:”, k, “v:”, v) } } Output: m: map[boo:map[second:2] foo:map[first:1]] k: boo v: map[second:2] k: foo … Read more

Shallow copy of a Map in Java

It’s always better to copy using a copy constructor. clone() in Java is broken (see SO: How to properly override clone method?). Josh Bloch on Design – Copy Constructor versus Cloning If you’ve read the item about cloning in my book, especially if you read between the lines, you will know that I think clone … Read more

How to count items in a Go map?

Use len(m). From http://golang.org/ref/spec#Length_and_capacity len(s) string type string length in bytes [n]T, *[n]T array length (== n) []T slice length map[K]T map length (number of defined keys) chan T number of elements queued in channel buffer Here are a couple examples ported from the now-retired SO documentation: m := map[string]int{} len(m) // 0 m[“foo”] = … Read more

How can I use map and receive an index as well in Scala?

I believe you’re looking for zipWithIndex? scala> val ls = List(“Mary”, “had”, “a”, “little”, “lamb”) scala> ls.zipWithIndex.foreach{ case (e, i) => println(i+” “+e) } 0 Mary 1 had 2 a 3 little 4 lamb From: http://www.artima.com/forums/flat.jsp?forum=283&thread=243570 You also have variations like: for((e,i) <- List(“Mary”, “had”, “a”, “little”, “lamb”).zipWithIndex) println(i+” “+e) or: List(“Mary”, “had”, “a”, “little”, … Read more

UnmodifiableMap (Java Collections) vs ImmutableMap (Google) [duplicate]

An unmodifiable map may still change. It is only a view on a modifiable map, and changes in the backing map will be visible through the unmodifiable map. The unmodifiable map only prevents modifications for those who only have the reference to the unmodifiable view: Map<String, String> realMap = new HashMap<String, String>(); realMap.put(“A”, “B”); Map<String, … Read more

map vs. hash_map in C++

They are implemented in very different ways. hash_map (unordered_map in TR1 and Boost; use those instead) use a hash table where the key is hashed to a slot in the table and the value is stored in a list tied to that key. map is implemented as a balanced binary search tree (usually a red/black … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)