Posts

Showing posts from September, 2022

Designing a Good Custom Key for HashMap - equal() , hashcode() implementation example

  Designing a Good Custom Key for HashMap Can we use an object as a key for a   HashMap   in Java? This is a very   popular interview question   indeed. It is asked immediately after “how a HashMap works?”. Let’s make reasoning around a user-defined class as a   key in hashmap in Java . Table Of Contents 1. The Key should Honor the Contract between hashCode() and equals() 2. What if Changing the Key’s HashCode is Allowed? 3. We should Make the HashMap’s Key Immutable 4. HashMap Custom Key Example 5. Demo 6. Conclusion 1. The Key should Honor the Contract between  hashCode()  and  equals() The very basic need for designing a good key is that “ we should be able to retrieve the value object back from the map without failure “, otherwise no matter how fancy data structure you build, it will be of no use. To decide that we have created a good key, we MUST know “ how HashMap works? “. I will leave, how hashmap works, part to you to read from the l...

Response Code

Image
 

How to Run/Deploy Spring Boot & MySQL in Docker

Image
  Spring Boot on Docker connecting to MySQL Docker container Use MySQL Image published by Docker Hub ( https://hub.docker.com/_/mysql/ ) Command to run the mysql container  docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:5.6 In the Spring Boot Application, use the same container name of the mysql instance in the application.properties  spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test Create a  Dockerfile  for creating a docker image from the Spring Boot Application  FROM openjdk:8 ADD target/users-mysql.jar users-mysql.jar EXPOSE 8086 ENTRYPOINT ["java", "-jar", "users-mysql.jar"] Using the Dockerfile create the Docker image. From the directory of Dockerfile -  docker build . -t users-mysql Run the Docker image (users-mysql) created in #4.  docker run -p 8086:8086 --name users-mysql --link mysql-standalone:mysql -d users-mysql Useful Docke...

Memory Leaks in Java

Image
  Memory Leaks in Java 1. Introduction One of the core benefits of Java is the automated memory management with the help of the built-in Garbage Collector (or  GC  for short). The GC implicitly takes care of allocating and freeing up memory, and thus is capable of handling the majority of memory leak issues. While the GC effectively handles a good portion of memory, it doesn't guarantee a foolproof solution to memory leaking. The GC is pretty smart, but not flawless. Memory leaks can still sneak up, even in the applications of a conscientious developer. There might still be situations where the application generates a substantial number of superfluous objects, thus depleting crucial memory resources, and sometimes resulting in the whole application's failure. Memory leaks are a genuine problem in Java. In this tutorial, we'll learn  what the potential causes of memory leaks are, how to recognize them at runtime, and how to deal with them in our application . 2. What ...