In this article , we will learn how to sort a Map in java.

Ok. Lets Get Started.

Before Java8 how we can sort the Map (If Map key is primitive data type):
— — — — — — — — — — — — — — — — — — — — — — — — — —
HashMap<Integer, String> mapOfNames = new HashMap<>();
mapOfNames.put(1, “karan”);
mapOfNames.put(5, “vivek”);
mapOfNames.put(19, “sumit”);
mapOfNames.put(8, “abc”);

Set<Entry<Integer, String>> entrySet = mapOfNames.entrySet();
List<Entry<Integer, String>> list = new ArrayList<>(entrySet);

Collections.sort(list, new Comparator<Entry<Integer, String>>() {
public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {

In this article, we will learn how to sort a list in Java.

Ok. Lets Get Started.

Before Java8 how we sort the list :
— — — — — — — — — — — — — — —

List<Integer> numbers = new ArrayList<>();

Collections.sort(numbers);// elements will be print in ascending order
System.out.println(“Ascending order : “ + numbers);

So here it will print the list in ascending order. I f you want to print the list in descending order then you need to call reverse() method of Collections class.

Collections.reverse(numbers); // elements will be…

How to use map(), filter() and reduce() methods on java8 stream.

map(), reduce() and filter() are intermediate operations to transforming and filtering the elements. With these three methods we can do a lot, so its worth knowing them intimately.

These three methods we can apply only on stream. In our last article, we have already discussed about Stream in java. So once you have a stream object, you can use variety of methods to transform it into another stream object.

1. Map():

In simple words, the map() method is used to transform one object into another object. It takes a lambda…

Sequential Stream vs Parallel Stream in java

In this article we will learn java 8 stream with example.

What is Stream in java?
A Stream is not a data structure instead it takes input from the collection , Arrays.
A Stream in java is a sequence of objects which operates on data source such as an array or collection. Stream also supports many aggregate operations like filter, map, limit and reduce to customize the original data into a different form according to the need.

Sequential Stream:
Sequential stream are non-parallel streams that use only single thread to process the pipelining. It…

Optional was introduced in jdk8. It is a public final class and used to avoid NullPointerException in Java application.

It is a public final class which is used to avoid NullPointerException in java application. It was introduced in Java8. As we know java is an object oriented programming language . So sometime object contain some value or sometime null. To avoid NullPointerException we can use Optional class.

Okay, Lets create one java class to check usage of Optional class in java application.

First I will create one model class i.e. It containing 3 fields id, name and city.


As we know set will never contain duplicate elements.
But how in java Set interface implemented class HashSet achieve this uniqueness. In this post, we will discuss about this.

HashSet uses HashMap internally to store it’s objects. Every constructor of HashSet class internally creates one HashMap object.

* Constructs a new, empty set; the backing HashMap instance has
* default initial capacity (16) and load factor (0.75).
public HashSet() {
map = new HashMap<>();

Whenever you create a HashSet object, one HashMap object associated with it is also created. …

How Spring Boot Transaction Management Works with the @Transactional annotation and Propagation types.

The process of combining related operations into single unit and executing them by applying do everything or nothing principle is called Transaction Management.

We execute sensitive logics like ticket booking, transfer money and etc. logics by enabling transaction management support.




  • If the addMessage() is called directly then it will create its own Transaction.
  • If the add message is called from another service –
    a. If…

In this story we will learn how to use Profiles in Spring Boot Application.

Every enterprise application has many environments, like:

Dev | Test | Stage | Prod | UAT / Pre-Prod

Each environment requires a configurations that is specific to them. These environments host specific configurations called Profiles.

In this story I will create a simple spring boot project and will demonstrate how to setup Profiles for your Spring Boot application.

Let’s start with setting up a Spring Boot application from the

Now Click On Generate then zip file of source code will be generated . …

Most Expected Interview questions.


ANS : It is an open source, light weight, non-invasive, java based ORM Framework. To develop Object based Database Software independent O-R Mapping, persistence logic in all kind of java, JEE and Java Framework apps.



1. Hibernate gives HQL (Hibernate Query Language) as DataBase Software Independent.

2. Hibernate supports lazy loading.

3. Allows to develop objects based persistence logic in standalone environment and also in web application environment.

4. Does not need the heavy weight containers …

Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store