Conversion of List to Map in Java

List v/s Map:

Before starting with List to Map conversion, we should understand difference between List and Map

List stores group of objects as single unit/entity where it allows duplicates objects maintaining insertion order

Map stores group of key-value pairs as single unit/entity where keys must be unique and values can be duplicated

So, we need to decide what we want to put inside Map as key and value. Ideally, whenever list stores group of some objects having 2 or more attributes then we can simply choose any 2-attributes for key and value while putting inside Map.

Examples :

We will cover 2 examples as described below,

Id can be chosen as key and any other attribute like name can selected for value