Java Login


Hello guys, is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.

     << Previous
Next >>     

Spring Collections

Collections are very useful and frequently used with java technology. Generics also made it very easy to use collection with auto-boxing and un-boxing functionality. Collections are used to manipulate objects easily with java. In spring, collections are used to manage objects instead of using primitive data types. This tutorial explains how to use the collections API with Spring configurations.

Before collection, we used to pass single value at a time. If we are required to pass multiple or bundle of data to be passed like list, set, and map etc. To manage such kind or problem, spring provides a way of injecting collections. We can inject list, map, set and pros type collections in spring. There are separate tags are used for these type of collection types as listed:

  • <list> - This helps in wiring ie injecting a list of values, allowing duplicates.
  • <set> - This helps in wiring a set of values but without any duplicates.
  • <map> - This can be used to inject a collection of name-value pairs where name and value can be of any type.
  • <props> - This can be used to inject a collection of name-value pairs where the name and value are both Strings.

Example of Collection Injection in Spring

package springCollection;

import java.util.*;

public class Department {
private List<String> departmentList;
private Set<String> departmentSet;
private Map<String> departmentMap;
private Properties departmentProperties;

public void setDepartmentList(List<String> departmentList) {
this.departmentList = departmentList;
public void setDepartmentSet(Set<String> departmentSet) {
this.departmentSet = departmentSet;
public void setDepartmentMap(Map<String> departmentMap) {
this.departmentMap = departmentMap;
public void setDepartmentProperties
(List<String> departmentProperties) {

this.departmentProperties = departmentPropertiest;

public void displayContent(){
System.out.println("List :: "+departmentList);
System.out.println("List :: "+departmentSet);
System.out.println("List :: "+departmentMap);
System.out.println("List :: "+departmentProperties);




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="department" class="springCollection.Department">
<property name="departmentList" >

<property name="departmentSet" >

<property name="departmentMap" >
<entry key="1" value="ADMIN"/>
<entry key="2" value="MARKETING"/>
<entry key="3" value="SECURITY"/>

<property name="departmentProperties" >
<prop key="1">"MANAGEMENT"</prop>
<prop key="2">"FINANCE"</prop>
<prop key="3">"IT"</prop>



package springCollection;

import org.springframework.context.ApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext

Department obj = (Department)context.getBean("department");

     << Previous
Next >>