Spring Dependency Injection With Map Collection Property

Spring » on Aug 9, 2011 { 7 Comments } By Sivateja

Map will stores the data in key, value base that to key, value must be the objects of any type.  One [ key, value ] is called one pair or one entry.


See here we have two entries [two pairs], one entry is k1  — 100 and second entry is k2   — 200

  • Let us come to the concept, Map.Entry will gives one entry
  • Map is the interface, Entry is the static class in Map interface, so we can call Map.Entry just remember this concept as of now
  • Actually In this spring we always use Map.Entry class object only

In the XML

      <entry key="k1">

      <entry key="k2">

      <entry key="k3">

      <entry key="k4" value-ref="id2" />

See in the xml, we have to configure like this, so we have 3 pairs [ key, values] means spring container will creates 3 Map.Entry class objects and stores those objects in <map />

One <entry /> tag will produces one object of Map.Entry, by the way k4 is to getting other bean class object.

Complete Example Using Map Collection

files will be used..

  • spconfig.xml


Directory structure:

package java4s;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class WelcomeBean {

	private Map data;	  

	public void setData(Map data) { = data;

	public void show()
		   Set s=data.entrySet();
		   Iterator it = s.iterator();
			   Map.Entry me = (Map.Entry);
			   System.out.println(me.getKey()+ " - "+me.getValue());




<bean id="id1" class="java4s.WelcomeBean">

<property name="data" >
       <entry key="k1">
       <entry key="k2">
       <entry key="k3">


package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class ClientLogic {

	public static void main(String[] args)
		Resource res = new ClassPathResource("spconfig.xml");
		BeanFactory factory = new XmlBeanFactory(res);

		Object o = factory.getBean("id1");
		WelcomeBean wb = (WelcomeBean)o;;




  •  First take our, see line number 15 when we call factory.getBean(“id1”) then spring IOC will go to spconfig.xml and verify the class name having id1,  [ you can see this in spconfig.xml line number 5, there our class name is WelcomeBean ]
  • Now it will jumps to
  • In we have been taken Map property [ see line number 7 ], and written setter property for that Map
  • In spconfig.xml we configured some values for that Map property right, so with this setter method spring IOC will inject all these values into Map data property
  • now come to show() method at line number 13, here see the concept actually we don’t have any iterator for Map right, hope you know this fact 🙂  , but we need iterator to get and print the data from the Map, moreover List,Set have the iterator.
  • So now we need Set or List to get the iterator,  so am going to take Set, at line number 15, of course you can go with List as well
  • Actually data is of Map type, in Map we have one method called entrySet() which always returns Set object, so i got the iterator by using this Set object at line number 16
  • Now see line number 19, i have been typecast into Map.Entry, so that i can print key and values separately by calling getKey() and getValues() methods in the Map.Entry Class [ I already given the concept about this Map.Entry go to top and check if you have any doubt regarding this ]
  • And we done 🙂

And mates this Map having much importance in java remember….!!!!!!



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


7 Responses to “Spring Dependency Injection With Map Collection Property”
  1. Hi Sir,

    I think Entry is not a class …it is an static interface inside Map interface…

  2. Java4s says:


    Its class only, you can check this for your reference.

  3. Gautam says:

    dude we can iterate over map like this:

    for(String key : map.keySet()) {
    System.out.println(key+” “+map.get(key));

    • Chandrakanth says:

      hahaaa obviously 😉
      in our situation we use like >>

      for (Object key : data.keySet()) {
      System.out.println("key = "+key + " value = " + data.get(key));

  4. Arpit says:

    Hi Sivteja,

    Thanks for the simplest tutorial on spring.
    I think from JDK1.7 Map.Entry is interface. Please have a look

  5. Shahbaaz Khokhar says:

    Exception in thread “main” java.lang.NullPointerException showing error for me on iterator snipper in Bean

  6. Prasanna Danda says:

    we can iterate over map like this:
    Iterator<Map.Entry> it=studentData.entrySet().iterator();
    System.out.println("Map "+entries.getKey() +"value" +entries.getValue() );


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.