spring入门-----spring中遍历各种集合

spring-collection.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.     <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->  
  7.     <bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean" scope="singleton" lazy-init="default">  
  8.         <!-- set集合 -->  
  9.         <property name="sets">  
  10.             <set>  
  11.                 <value>岑红军</value>  
  12.                 <value>军哥</value>  
  13.                 <value>哈哈</value>  
  14.                 <value>呵呵</value>  
  15.                 <value>嘿嘿</value>  
  16.                 <value>洗洗</value>  
  17.             </set>  
  18.         </property>  
  19.     <!-- list集合 -->     
  20.     <property name="users">  
  21.         <!-- <list>  
  22.             <ref bean="u1"/>  
  23.             <ref bean="u2"/>  
  24.             <ref bean="u3"/>  
  25.             <ref bean="u4"/>  
  26.         </list> -->  
  27.         <array>  
  28.             <ref bean="u1"/>  
  29.             <ref bean="u2"/>  
  30.             <ref bean="u3"/>  
  31.             <ref bean="u4"/>  
  32.         </array>  
  33.     </property>  
  34.     <!-- map -->  
  35.     <property name="map">  
  36.         <map>  
  37.             <entry key="1" value-ref="u1"></entry>  
  38.             <entry key="2">  
  39.                 <ref bean="u2"/>  
  40.             </entry>  
  41.             <entry key="3" value-ref="u3">  
  42.             </entry>  
  43.           
  44.         </map>  
  45.     </property>  
  46.       
  47.     <property name="props">  
  48.         <props>  
  49.             <prop key="1">jdbc:oracle</prop>  
  50.             <prop key="2">jdbc:mysql</prop>  
  51.             <prop key="3">jdbc:access</prop>  
  52.         </props>  
  53.     </property>  
  54.     </bean>  
  55.     <bean id="u1" class="www.csdn.spring.collection.set.User">  
  56.         <property name="name" value="deep" />  
  57.         <property name="age" value="21" />  
  58.     </bean>  
  59.     <bean id="u2" class="www.csdn.spring.collection.set.User">  
  60.         <property name="name" value="deepsoul" />  
  61.         <property name="age" value="22"></property>  
  62.     </bean>  
  63.     <bean id="u3" class="www.csdn.spring.collection.set.User">  
  64.         <property name="name" value="chrp" />  
  65.         <property name="age" value="23" />  
  66.     </bean>  
  67.     <bean id="u4" class="www.csdn.spring.collection.set.User">  
  68.         <property name="name" value="redarmy" />  
  69.         <property name="age" value="28" />  
  70.     </bean>  
  71.       
  72.       
  73.   
  74. </beans>  

User.java

[java]  view plain copy
  1. package www.csdn.spring.collection.set;  
  2.   
  3. public class User {  
  4.     private String name;  
  5.     private Integer age;  
  6.     public void setName(String name) {  
  7.         this.name = name;  
  8.     }  
  9.     public void setAge(Integer age) {  
  10.         this.age = age;  
  11.     }  
  12.       
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public Integer getAge() {  
  17.         return age;  
  18.     }  
  19.     @Override  
  20.     public String toString() {  
  21.         return "User [name=" + name + ", age=" + age + "]";  
  22.     }  
  23.       
  24.       
  25. }  

Collection.java

[java]  view plain copy
  1. package www.csdn.spring.collection.set;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.Properties;  
  6. import java.util.Set;  
  7.   
  8. public class CollectionBean {  
  9.     //set集合  
  10.         public Set<String> sets;  
  11.   
  12.         public void setSets(Set<String> sets) {  
  13.             this.sets = sets;  
  14.         }  
  15.           
  16.         public CollectionBean() {  
  17.             super();  
  18.             System.out.println("========================");  
  19.         }  
  20.           
  21.         //list集合  
  22.         public List<User> users;  
  23.         public void setUsers(List<User> users) {  
  24.             this.users = users;  
  25.         }  
  26.           
  27.         //map集合  
  28.         public Map<Integer,User> map;  
  29.   
  30.         public void setMap(Map<Integer, User> map) {  
  31.             this.map = map;  
  32.         }  
  33.           
  34.         //props集合  
  35.         public Properties props;  
  36.   
  37.         public void setProps(Properties props) {  
  38.             this.props = props;  
  39.         }  
  40.           
  41. }  

TestBean.java

[java]  view plain copy
  1. package www.csdn.spring.collection.set;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7. import java.util.Properties;  
  8. import java.util.Set;  
  9.   
  10. import org.junit.Test;  
  11. import org.springframework.context.ApplicationContext;  
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  13.   
  14. public class TestBean {  
  15.       
  16.     @Test  
  17.     public void tests(){  
  18.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  19.                 "classpath:spring-collection.xml");  
  20.         CollectionBean bean = context.getBean("collectionBean",  
  21.                 CollectionBean.class);  
  22.         System.out.println("---------------set----------------");  
  23.         //获取set集合  
  24.         Set<String> sets=bean.sets;  
  25.         //得到迭代器  
  26.         Iterator<String> it=sets.iterator();  
  27.         //遍历  
  28.         while (it.hasNext()){  
  29.             System.out.println(it.next());  
  30.         }  
  31.           
  32.           
  33.         System.out.println("--------------list----------------");  
  34.         List<User> users = bean.users;  
  35.         for (User u : users) {  
  36.             System.out.println(u.getName() + "-------------" + u.getAge());  
  37.         }  
  38.           
  39.         System.out.println("----------------map1---------------");  
  40.         //map1  
  41.         Map<Integer,User> map=bean.map;  
  42.         //得到map集合的key键值的set集合  
  43.         Set<Integer> setkeys=map.keySet();  
  44.         //得到key键值set集合的迭代器  
  45.         Iterator<Integer> itkeys=setkeys.iterator();  
  46.         //迭代键值  
  47.         while(itkeys.hasNext()){  
  48.             //得到一个具体的键值  
  49.             Integer key = itkeys.next();  
  50.             //通过map集合的get(key)方法 获取key键值对应的value值  
  51.             User user =map.get(key);  
  52.             System.out.println(key+"------"+user.getName()+"-----"+"------"+user.getAge());  
  53.         }  
  54.           
  55.         System.out.println("--------------map2--------------");  
  56.         //map2  
  57.         //获取实体对象的set集合  
  58.         Set<Entry<Integer,User>> setentry=map.entrySet();  
  59.         //获取实体对象的迭代器  
  60.         Iterator<Entry<Integer, User>> itentry =setentry.iterator();  
  61.         //迭代  
  62.         while(itentry.hasNext()){  
  63.             //得到具体的Entry对象  
  64.             Entry<Integer,User> entry=itentry.next();  
  65.             //通过entry对象的getKey()和getValue得到  
  66.             System.out.println(entry.getKey()+"----"+entry.getValue().getName()+"------"+entry.getValue().getAge());  
  67.         }  
  68.           
  69.           
  70.         System.out.println("--------------props-------------");  
  71.         Properties props = bean.props;  
  72.           
  73.         //得到这个结合键值的key的set集合  
  74.         Set<String> setprops = props.stringPropertyNames();  
  75.         //String集合迭代器  
  76.         Iterator<String> keystr = setprops.iterator();  
  77.           
  78.         while(keystr.hasNext()){  
  79.             //具体键值  
  80.             String key = keystr.next();  
  81.             //getProperty(key)获取key对应的value值  
  82.             System.out.println(key+"-----"+props.getProperty(key));  
  83.         }  
  84.     }  
  85. }  

控制台输出:

2013-4-25 10:09:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@50c4fe76: startup date [Thu Apr 25 10:09:49 CST 2013]; root of context hierarchy
2013-4-25 10:09:49 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-collection.xml]
2013-4-25 10:09:49 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3ff2cea2: defining beans [collectionBean,u1,u2,u3,u4]; root of factory hierarchy
========================
---------------set----------------
岑红军
军哥
哈哈
呵呵
嘿嘿
洗洗
--------------list----------------
deep-------------21
deepsoul-------------22
chrp-------------23
redarmy-------------28
----------------map1---------------
1------deep-----------21
2------deepsoul-----------22
3------chrp-----------23
--------------map2--------------
1----deep------21
2----deepsoul------22
3----chrp------23
--------------props-------------
3-----jdbc:access
2-----jdbc:mysql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值