JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?


Answer:

The "scalability issues" for Hashtable are present in exactly the same way in Collections.synchronizedMap(Map) - they use very simple synchronization, which means that only one thread can access the map at the same time.

This is not much of an issue when you have simple inserts and lookups (unless you do it extremely intensively), but becomes a big problem when you need to iterate over the entire Map, which can take a long time for a large Map - while one thread does that, all others have to wait if they want to insert or lookup anything.

The ConcurrentHashMap uses very sophisticated techniques to reduce the need for synchronization and allow parallel read access by multiple threads without synchronization and, more importantly, provides an Iterator that requires no synchronization and even allows the Map to be modified during interation (though it makes no guarantees whether or not elements that were inserted during iteration will be returned).

Here are the differences:

     Property           HashMap      Hashtable   ConcurrentHashMap  
Null as a key Allowed Not Allowed Not Allowed
Is thread-safe No Yes Yes
Lock mechanism N/A Locks the whole map Locks the portion
Iterator Fail-fast Fail-fast Fail-safe

<< Previous Question | Next Question >>

List Of Java Interview Questions:

  1. Previous set of java interview questions
  2. What is difference between Lambda Expression and Anonymous class?
  3. What is HTTP basic authentication?
  4. What is functional interface in java?
  5. What is the difference between HTTP methods GET and POST?
  6. What is difference between CountDownLatch and CyclicBarrier in Java?
  7. Can Enum extend any class in Java?
  8. Can Enum implements any interface in Java?
  9. Can we have constructor in abstract class?
  10. What is MVC pattern?
  11. What is ActionServlet in struts?
  12. What is the difference between servlet & Filter?
  13. What is ActionMapping in struts?
  14. What is the difference between application server and web server?
  15. What is the difference between JPA and Hibernate?
  16. What is difference between the Value Object and JDO?
  17. How Struts control data flow?
  18. What is Spring?
  19. What is Dependency Injection?
  20. What are the different types of dependency injections in spring?
  21. What is BeanFactory in Spring?
  22. What is difference between BeanFactory and ApplicationContext in spring?
  23. How to make a bean as singleton in spring?
  24. What is IOC or inversion of control?
  25. What are different types of spring auto-wiring modes?
  26. What are the limitations and disadvantages of spring autowiring?
  27. Is the spring singleton bean thread safe?
  28. Why ConcurrentHashMap is faster than Hashtable in Java?
  29. What is the difference between ConcurrentHashMap and Hashtable in Java?
  30. Difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
  31. What is the difference between ORM, JPA and Hibernate?
  32. What is stream pipelining in Java 8?
  33. What is interface default method in java 8?
  34. Java-8: Interface with default methods vs Abstract class.
  35. What is @SpringBootApplication annotation in Spring boot project?
  36. What Embedded Containers Does Spring Boot Support?
  37. What are the advantages and disadvantages of Spring Boot?
  38. What is Spring Boot Actuator?
  39. What is Spring Boot Initializr?
  40. How does Maven resolve version conflicts of dependencies?
  41. How to reload Spring Boot Application without restarting server?
  42. What is the difference between Spring Boot and the Spring framework?
  43. What are the key components of Spring Boot framework?
  44. What are the different types of bean scope in Spring framework?
  45. What are the standard Spring build-in events?
  46. What is Spring IoC container?
  47. Differences between BeanFactory and the ApplicationContext in Spring framework.
  48. Difference between constructor injection and setter injection in Spring.
  49. How Java-8 Streams differ from collections
  50. What are the various ways to obtain Streams in Java-8?
  51. List Java-8 Streams intermediate operations.
  52. List Java-8 Streams terminal operations.
  53. Can we reuse Java-8 Streams?
  54. Difference between map and flatMap methods in Java 8
  55. What is the difference between Closure and Lambda in Java 8?
  56. Does Java 8 Lambda supports recursive call?
  57. Can Java 8 default methods override equals, hashCode and toString?
  58. Hibernate Eager vs Lazy Fetch Type
  59. What is POJO?
  60. What is HQL (Hibernate Query Language)?
Knowledge Centre
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.