Skip to main content

Posts

How to implement cache in Java

According to wikipedia, a cache is a hardware or software component that stores data so that future requests for that data can be served faster.Benefits:Faster access of data in O(1)Computation complexity once for the first timeTypes:Memory cacheDatabase cacheDisk cache, etcIn this post, we’ll go through the steps to create a in memory cache in java.To create a cache, we can simply use a map / dictionary data structure and we can get the expected result of O(1) for both get and put operation.But, we can’t store everything in our cache. We have storage and performance limits.A cache eviction algorithm is a way of deciding which element to evict when the cache is full. To gain optimized benefits there are many algorithms for different use cases.Least Recently Used (LRU)Least Frequently Used (LFU)First In First Out (FIFO)Last In First Out (LIFO) etc.There are only two hard things in computer science, Cache invalidation and naming things. — Phil KarltonIn our design, we will useHashM…

HashMap implementation for Java

HashMap is a dictionary data structure provided by java. It’s a Map-based collection class which is used to store data in Key & Value pairs. In this article we’ll be creating our own hashmap implementation.The benefit of using this data structure is faster data retrieval. It has data access complexity of O(1) in best case.In layman’s terms, a for each key we get the associated value.To implement this structure,We need a list to store all the keysKey — Value relationship to get value based on keyWe can have a list containing all the key, values and to access we need to search all of it.But the main point of hash map is to access the keys faster in 0(1) access time.Here, hashing comes into play. We can hash the key and relate it with the index to retrieve data faster.Hash comes with a problem too, collision. It is always recommended to use a better hash function that can reduce chances of collision.Multiple hash can have same hash key. For that reason, there is a bucket or container…

Product of Array Except Self

Given an arraynumsofnintegers wheren> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Example:Input: [1,2,3,4]
Output: [24,12,8,6]Solution:Let’s start with a brute-force solution.public int[] productExceptSelf(int[] nums) {
int[] arr = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
int mul = 1;
for (int j = 0; j < nums.length; j++) {
if(i == j) {
continue;
}
mul *= nums[j];
}
arr[i] = mul;
}
return arr;
}Clearly, this is not an efficient solution and the time complexity for this solution is O(n²)We can improve the implementation using the help of division. We can get the total multiplication value and for each element we divide the item.public int[] productExceptSelf(int[] nums) {
int mul = 1;
for(int i = 0; i < nums.length; i++) {
mul *= nums[i];
}

for (int i = 0; i < nums.length; i++) {

Java Heap Dump

Overview A heap dump is a snapshot of all the objects that are in memory in the JVM at a certain moment. They are very useful to troubleshoot memory-leak problems and optimize memory usage in Java applications.
Heap dumps are usually stored in binary format hprof files.

Java Out Of Memory Error

java.lang.OutOfMemoryError is thrown when there is insufficient space to allocate an object. That is, garbage collection cannot make any further space available to accommodate a new object, and the heap cannot be further expanded.

An OutOfMemoryError does not necessarily imply a memory leak. The issue might simply be a configuration issue, for example, if the specified heap size (or the default size if not specified) is insufficient for the application.

Code Smell বা বাজে কোড

Code Smell বা বাজে কোড বোঝার কিছু প্যাটার্ন আছে যা দেখে বোঝা যায়। এখানে এই সম্পর্কে কিছু ধারনা দেয়া হল। code smell সাধারনত ২ ভাগে বিভক্ত। Code smells within classesCode smells between classes