Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, April 30, 2015

Java Collections Cheat Sheet

HashMap internal implementation in Java

HashSet internal implementation in Java

HashMap.put method will return
 
  oldValue: if key is already present, then it will overwrite the value and returns old value
  null: if key is not present, then it will add this key,value pair and then returns null.

In case of HashSet, internally it has a hashmap. When we call add method it will call 'put' method of the hashmap and if put method returns null means new entry is added into hashmap and hashset

more at: http://javahungry.blogspot.com/2013/08/how-sets-are-implemented-internally-in.html

Java purpose of AtomicInteger

We can use AtomicInteger to avoid count++ issue in mutli-threading environment.

AtomicInteger provides incrementAndGet() and decrementAndGet() methods which are thread safe.

With use of AtomicInteger, we can solve this problem without using synchronized keyword

more: https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html

          http://javarevisited.blogspot.in/2012/01/how-to-write-thread-safe-code-in-java.html