In any application, there is a common requirement to use the Map. In normal Java applications, we can create the Map object and we can use that map where ever we want. But in spring, it is quite different the dependencies will be created and injected by the spring container.

In the previous tutorial, we have discussed the Spring Collection Dependency List Example, this tutorial tells you how to define the java.util.Map collection and how to pass the values to Map collection through DI spring.

Spring Collection Map Dependency :

In a spring bean, if a dependency is of collection type java.util.Map, then in spring configuration file we need to configure the Map collection with <map> tag.

A Map is a collection of entries, and each entry is a collection of key-value pairs. To configure the entries and key values, spring provides an <entry> tag to represent the Map entry. And <entry> tag has two properties key and value <entry key=" " value=" "/>

Here is the example to handle the spring collection Map.

Example for Spring Collection Map Dependency :

Project Structure :

Spring Collection Map Dependency

Spring Maven Dependency :

<!-- Spring -->

<!-- Logging with SLF4J & LogBack -->

Spring Beans:
package com.onlinetutorialspoint.bean;

public class Employee {
    private int empId;
    private String empName;

    public int getEmpId() {
        return empId;

    public void setEmpId(int empId) {
        this.empId = empId;

    public String getEmpName() {
        return empName;

    public void setEmpName(String empName) {
        this.empName = empName;

package com.onlinetutorialspoint.bean;

public class Department {
    private int deptId;
    private String deptName;

    public int getDeptId() {
        return deptId;

    public void setDeptId(int deptId) {
        this.deptId = deptId;

    public String getDeptName() {
        return deptName;

    public void setDeptName(String deptName) {
        this.deptName = deptName;

package com.onlinetutorialspoint.bean;

import java.util.Map;

public class DepartmentMap {
    private Map < Integer, Employee > deptMap;

    public Map < Integer, Employee > getDeptMap() {
        return deptMap;

    public void setDeptMap(Map < Integer, Employee > deptMap) {
        this.deptMap = deptMap;



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:context="" xmlns:xsi="" xsi:schemaLocation="">
   <bean id="deptBean" class="com.onlinetutorialspoint.bean.Department">
      <property name="deptId" value="20501" />
      <property name="deptName" value="IT" />
   <bean id="deptBean2" class="com.onlinetutorialspoint.bean.Department">
      <property name="deptId" value="20502" />
      <property name="deptName" value="IT" />
   <bean id="deptBean3" class="com.onlinetutorialspoint.bean.Department">
      <property name="deptId" value="20503" />
      <property name="deptName" value="IT" />
   <bean id="empBean" class="com.onlinetutorialspoint.bean.Employee">
      <property name="empId" value="101" />
      <property name="empName" value="Chandra Shekhar" />
   <bean id="empBean2" class="com.onlinetutorialspoint.bean.Employee">
      <property name="empId" value="102" />
      <property name="empName" value="Robert" />
   <bean id="empBean3" class="com.onlinetutorialspoint.bean.Employee">
      <property name="empId" value="103" />
      <property name="empName" value="John" />
   <bean id="deptMap" class="com.onlinetutorialspoint.bean.DepartmentMap">
      <property name="deptMap">
            <entry key="#{deptBean.deptId}" value-ref="empBean" />
            <entry key="#{deptBean2.deptId}" value-ref="empBean2" />
            <entry key="#{deptBean3.deptId}" value-ref="empBean3" />

On the above configuration to access the deptBean properties, we use the Spring Expression Language support (“#{deptBean.deptId}”) .

Run the Application :
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;

import com.onlinetutorialspoint.bean.DepartmentMap;
import com.onlinetutorialspoint.bean.Employee;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("springconfiguration.xml");
        DepartmentMap deptMap = (DepartmentMap) context.getBean("deptMap");

        Map < Integer, Employee > map = deptMap.getDeptMap();

        Set < Integer > keySet = map.keySet();
        Iterator  it = keySet.iterator();
        while (it.hasNext()) {
            Integer key = (Integer);
            System.out.println("DeptId : " + key + " Employees Information");
            Employee emp = map.get(key);
            System.out.println("EmployeeId : " + emp.getEmpId());
            System.out.println("EmployeeName : " + emp.getEmpName());




Output :

DeptId : 20501 Employees Information
EmployeeId : 101
EmployeeName : Chandra Shekhar
DeptId : 20502 Employees Information
EmployeeId : 102
EmployeeName : Robert
DeptId : 20503 Employees Information
EmployeeId : 103
EmployeeName : John

Happy Learning 🙂

Download Example