Convert List to Map in Java

In this tutorial, we will learn the logic for converting a List into a Map in Java. We will also implement a Java program that will demonstrate the conversion.

Conversion of List representing the Job-Salaries of employees into a Map of key-value pair

What is a List?

List represents the ordered sequence of elements. List belongs to the java.util.List library module of Java. Elements from the list can be inserted, traversed, deleted, etc. The elements in the list are ordered in sequential manner that is why this data structure is called as List.

Each element in a List has an index which begins from zero, the further elements in the list has indexing i+1, and so on. We can add any Java object to a List. If the List is not of specific class-type, using Java Generics, the programmer can mix objects of different types (classes) in the same List. Mixing objects of different class types in the same List is not a good programming practice.

 

What is a Map?

A map is an object that stores the Key-Value pair of data. A map cannot contain duplicate values. Each key in map object can map to maximum only one value. There are three types of maps, they are HashMap, TreeMap, and LinkedHashMap.

The basic operations of Map are put(), get(), containskey(), containsvalue(), size() and isEmpty().

In Java, Map doesn’t allows the programmer to have the duplicate keys, but it allows to have duplicate values. HashMap and LinkedHashMap allows the programmer to have null keys and values in the map, but TreeMap doesn’t allow null key or value in the map.

Following is the Java code implementation of List to Map conversion:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class jobsalaries
{
  public String employee;
  public int salary;
    
  public jobsalaries(String employee, int salary) {
    this.employee = employee;
    this.salary = salary;
  }

  @Override
  public String toString() {
    return employee + "=" + salary;
  }
    
  public String  getemployee() {
    return employee;
  }

  public int getsalary() {
    return salary;
  }
  
  public static void main(String[] args)
  {
    // input list of Objects
    List<jobsalaries> js = new ArrayList<jobsalaries>();
    
    js.add(new jobsalaries("Software_Engineer", 80000));
    js.add(new jobsalaries("Business_Analyst", 120000));
    js.add(new jobsalaries("HR", 150000));
        
    Map<String, Integer> map = new HashMap<>();
                                               
    // construct key-value pairs from employee and salary fields of Job
    for (jobsalaries obj : js) {
      map.put(obj.getemployee(), obj.getsalary());
    }

    System.out.println("List : " + js);
    System.out.println("Map : " + map);
  }
  
}

Output:

https://drive.google.com/file/d/16jnUia7OO1X117G8D8XH2mWFjBf9Wju8/view?usp=sharing

 

Explanation:  In the above Java code I have demonstrated the List representing the job-salaries and converted it to a map. Beginning from the main class, I have declared a list of object js. Then further I have added few entries into the List. Then I have declared a resultant map of string type keys and integer type values. Then using a loop, the key value pairs have be formed by referring to the list of the Jobs and Salaries. The getemployee() and getsalary() are the mapper functions that assign the employee position accordingly to its respective salary.

Leave a Reply

Your email address will not be published. Required fields are marked *