Share on Google+Share on Google+

Spring Map Example

In this tutorial you will see an example of implement map in spring.

Spring Map Example

In this example you will see how bean is prepared for injecting Map collection type key and its values.


import java.util.Iterator;
import java.util.Map;

public class MapBean {
  private Map<String, Integer> student;
  public void setDetails(Map<String, Integer> student) {
  this.student = student;
  public void showDetails() {
     Iterator it = student.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry);
            System.out.println(pairs.getKey() " = " + pairs.getValue());


import org.springframework.beans.factory.BeanFactory;

public class MapBeanMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
    MapBean bean = (MapBeanbeanfactory.getBean("mapbean");


<bean id="mapbean" class="">
     <property name="details">
               <entry key="Satya" value="101"/>
               <entry key="Rohit" value="102"/>
               <entry key="Aniket" value="103"/>

When you run this application it will display output as shown below:

Satya = 101

Rohit = 102

Aniket = 103

Download this example code


Posted on: August 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.