Best way to create a hashmap of arraylist

You don’t need to re-add the ArrayList back to your Map. If the ArrayList already exists then just add your value to it. An improved implementation might look like: Map<String, Collection<String>> map = new HashMap<String, Collection<String>>(); while processing each line: String user = user field from line String value = value field from line Collection<String> … Read more

Hash table: why size should be prime? [duplicate]

The only reason is to avoid clustering of values into a small number of buckets (yes, distribution). A more even distributed hashtable will perform more consistently. from http://srinvis.blogspot.com/2006/07/hash-table-lengths-and-prime-numbers.html If suppose your hashCode function results in the following hashCodes among others {x , 2x, 3x, 4x, 5x, 6x…}, then all these are going to be clustered … Read more

Converting a Scala Map to a List

My favorite way to do this kind of things is like this: input.map { case (k,v) => (k.getBytes, v) }(collection.breakOut): List[(Array[Byte], List[Int])] With this syntax, you are passing to map the builder it needs to reconstruct the resulting collection. (Actually, not a builder, but a builder factory. Read more about Scala’s CanBuildFroms if you are … Read more

Is there a way to access the underlying container of STL container adaptors?

I spotted the following solution somewhere on the web and I’m using it in my projects: template <class T, class S, class C> S& Container(priority_queue<T, S, C>& q) { struct HackedQueue : private priority_queue<T, S, C> { static S& Container(priority_queue<T, S, C>& q) { return q.*&HackedQueue::c; } }; return HackedQueue::Container(q); } int main() { priority_queue<SomeClass> … Read more

C# flattening json structure

JObject jsonObject=JObject.Parse(theJsonString); IEnumerable<JToken> jTokens = jsonObject.Descendants().Where(p => !p.HasValues); Dictionary<string, string> results = jTokens.Aggregate(new Dictionary<string, string>(), (properties, jToken) => { properties.Add(jToken.Path, jToken.ToString()); return properties; }); I had the same requirement of flattening a nested json structure to a dictionary object. Found the solution here.