## TreeMap sort by values in java

Let’s learn can we sort TreeMap by values in java? TreeMap sort by values in java TreeMap stores key-value pairs which are in sorted order based on key. To sort a treemap by value we have to build some logic using comparator class. Here’s an example on how to sort a treemap by value. import…

Read More TreeMap sort by values in java

## TreeMap comparator() method in java with examples

Let’s learn TreeMap comparator() method in java with examples. Treemap comparator() method in java with examples TreeMap comparator() method returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys. Let’s see example on TreeMap comparator() method for natural ordering. import java.util.Comparator; import…

Read More TreeMap comparator() method in java with examples

## Difference between for and for each loop in java

Let’s learn what is the difference between for and for each loop in java? Difference between for and for each loop in java Here’s the difference between for loop and for-each loop in java. for loop for each loop Here in for loop we can increase counter as per our wish. Executes in a sequential…

Read More Difference between for and for each loop in java

## Java program to calculate average of 3 numbers

Let’s learn java program to calculate average of 3 numbers. Java program to calculate average of 3 numbers In program to find average of 3 numbers in java we are taking input from user using nextDouble() method of Scanner class and storing in three variables number1, number2 and number3. These variables are then passed to…

Read More Java program to calculate average of 3 numbers

## How to sort an array without using sort method in java

Let’s learn how to sort an array without using sort method in java? How to sort an array without using sort method in java In the below program on how to sort an array without using sort method we are not using Arrays.sort() method to sort given array. Meanwhile we are sorting array elements in…

Read More How to sort an array without using sort method in java

## Why is string immutable in java

Let’s learn why is string immutable in java example? Why is string immutable in java Why is string immutable? String is immutable in java because String objects are cached in string constant pool. String object once created cannot be changed i.e, when object is assigned to variable we cannot update the reference. If string object…

Read More Why is string immutable in java

## Sort characters alphabetically in a string in java

Let’s learn how to sort characters alphabetically in a string in java? Sort characters alphabetically in a string in java To sort string characters alphabetically we do not have any method in String class. So we are using different methods in sorting characters alphabetically in a string. So here the first method we are applying…

Read More Sort characters alphabetically in a string in java

## Reverse an array without using another array in java

Let’s learn how do you reverse an array without using another array java? Reverse an array without using another array in java To reverse an array without using another array we are using for loop which loops till middle index of given array and then swap first number with the last number, swap second number…

Read More Reverse an array without using another array in java