Hashtable, HashMap, HashSet , hash table concept in Java collection framework

Java’s Set and Map interfaces specify two very different collection types. A Set is just what it sounds like: a collection of distinct (non-equal) objects, with no other structure. A Map is, conceptually, also just what it sounds like: a mapping from a set of objects (the distinct keys) to a collection of objects (the values). Hashtable and HashMap both implement Map, HashSet implements Set, and they all use hash codes … Read more

Collision resolution in Java HashMap

When you insert the pair (10, 17) and then (10, 20), there is technically no collision involved. You are just replacing the old value with the new value for a given key 10 (since in both cases, 10 is equal to 10 and also the hash code for 10 is always 10). Collision happens when multiple keys hash to the … Read more

C# Java HashMap equivalent

Dictionary is probably the closest. System.Collections.Generic.Dictionary implements the System.Collections.Generic.IDictionary interface (which is similar to Java’s Map interface). Some notable differences that you should be aware of: Adding/Getting items Java’s HashMap has the put and get methods for setting/getting items myMap.put(key, value) MyObject value = myMap.get(key) C#’s Dictionary uses [] indexing for setting/getting items myDictionary[key] = value MyObject value = myDictionary[key] null keys Java’s HashMap allows null keys .NET’s Dictionary throws an ArgumentNullException if you try … Read more

C# Java HashMap equivalent

Dictionary is probably the closest. System.Collections.Generic.Dictionary implements the System.Collections.Generic.IDictionary interface (which is similar to Java’s Map interface). Some notable differences that you should be aware of: Adding/Getting items Java’s HashMap has the put and get methods for setting/getting items myMap.put(key, value) MyObject value = myMap.get(key) C#’s Dictionary uses [] indexing for setting/getting items myDictionary[key] = value MyObject value = myDictionary[key] null keys Java’s HashMap allows null keys .NET’s Dictionary throws an ArgumentNullException if you try … Read more

Exception in thread “main” java.lang.StackOverflowError

Your algorithm is fine. However int is too small for your computations, it fails for this input: At some point integer overflows to negative value and your implementation goes crazy, recursing infinitely. Change int num to long num and you’ll be fine – for some time. Later you’ll need BigInteger. Note that according to Wikipedia on Collatz conjecture (bold mine): The longest progression for … Read more