Sunday, July 9, 2023

Java (JVM) Memory Model - Switches


VM SwitchVM Switch Description
-XmsFor setting the initial heap size when JVM starts
-XmxFor setting the maximum heap size.
-XmnFor setting the size of the Young Generation, rest of the space goes for Old Generation.
-XX:PermGenFor setting the initial size of the Permanent Generation memory
-XX:MaxPermGenFor setting the maximum size of Perm Gen
-XX:SurvivorRatioFor providing ratio of Eden space and Survivor Space, for example if Young Generation size is 10m and VM switch is -XX:SurvivorRatio=2 then 5m will be reserved for Eden Space and 2.5m each for both the Survivor spaces. The default value is 8.
-XX:NewRatioFor providing ratio of old/new generation sizes. The default value is 2.

Wednesday, July 14, 2021

Consumer Example in Java 8


Consumer Example in Java 8 

forEach in interface which uses Consumer: 

default void forEach(Consumer<? super T> action) 



    for (T t : this) {




public interface Consumer<T>
Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

This is a functional interface whose functional method is accept(Object).


void accept(T t)
  • andThen

    default Consumer<T> andThen(Consumer<? super T> after)
    Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.
    after - the operation to perform after this operation
    a composed Consumer that performs in sequence this operation followed by the after operation
    NullPointerException - if after is null


import java.util.Arrays;

import java.util.List;

import java.util.function.Consumer;

public class ConsumerEx {

public static void main(String[] args) {

List<String> list = Arrays.asList("india", "uk", "fiji");

Consumer<String> consumer = (t) -> {










Tuesday, January 12, 2021

Docker: Important Docker Commands Help

Docker: Important Docker Commands Help


FROM adoptopenjdk/openjdk11:latest


ADD "target/docker-demo-0.0.1-SNAPSHOT.jar" "docker-demo.jar"

ENTRYPOINT ["java","-jar","docker-demo.jar"]

Docker Commands:

 Build Docker Image:

docker build -t docker-demo .

 Tag Docker Image: 

docker tag docker-demo dineshnithdocker/docker-demo

Push Docker Image to Docker Hub 

docker push dineshnithdocker/docker-demo

Check docker Images present in local: 

docker images

Remove Local Docker Images:  

docker rmi docker-demo dineshnithdocker/docker-demo

 Pull Docker Images from Docker Hub and run:

docker run -p 8080:8080 dineshnithdocker/docker-demo

Wednesday, November 25, 2020

Leet Code Problems Solutions

Intersection of Two Arrays I

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]


  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Code : Solution 1 - 

public class ArrayIntersection {

	public static int[]  intersect(int[] nums1, int[] nums2) {

		if (nums1.length > nums2.length) {
			return intersect(nums2, nums1);

		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		List<Integer> list = new ArrayList<Integer>();

		for (int i : nums1) {
			map.put(i, map.getOrDefault(i, 0) + 1);

		for (int i : nums2) {
			int count = map.getOrDefault(i, 0);
			if (count > 0) {
				map.put(i, count - 1);

		int[] result = new int[list.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = list.get(i);

		return result;

	public static void main(String... aa) {
		int[] nums1= {1,2,2,1};
		int[] nums2= {2,2};

Plus One

Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.


Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Example 3:

Input: digits = [0]
Output: [1]



  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9


Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <...


Search This Blog

Popular Posts