Scala: Using HashMap with a default value

Wow, I happened to visit this thread exactly one year after I posted my last answer here. 🙂 Scala 2.9.1. mutable.Map comes with a withDefaultValue method. REPL session: scala> import collection.mutable import collection.mutable scala> mutable.Map[Int, String]().withDefaultValue(“”) res18: scala.collection.mutable.Map[Int,String] = Map() scala> res18(3) res19: String = “”

Why is the initialCapacity of Hashtable 11 while the DEFAULT_INITIAL_CAPACITY in HashMap is 16 and requires a power of 2?

The following article addresses this question in some detail: HashMap requires a better hashCode() – JDK 1.4 Part II. According to that article, the main reason to move to power-of-two sizes was that bit masking is faster than integer division. This is not without adverse consequences, which are explained by one of the original authors: … Read more

Is the hashCode function generated by Eclipse any good?

You can see the implementation of hashCode function in java.util.ArrayList as public int hashCode() { int hashCode = 1; Iterator<E> i = iterator(); while (i.hasNext()) { E obj = i.next(); hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); } return hashCode; } It is one such example and your Eclipse generated code follows a … Read more

Simple hashmap implementation in C++

Most compilers should define std::hash_map for you; in the coming C++0x standard, it will be part of the standard library as std::unordered_map. The STL Page on it is fairly standard. If you use Visual Studio, Microsoft has a page on it. If you want to use your class as the value, not as the key, … Read more

Are final static variables thread safe in Java?

the reference to sharedData which is final is thread safe since it can never be changed. The contents of the Map is NOT thread safe because it needs to be either wrapped with preferably a Guava ImmutableMap implementation or java.util.Collections.unmodifiableMap() or use one of the Map implementations in the java.util.concurrent package. Only if you do … Read more

Using tuple in unordered_map

The template arguments for an unordered_map looks like this: template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_map; std::hash is not specialized for tuples (scroll down to Standard specializations for library types). Therefore you need to provide your own, something … Read more

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