11级_Java_曹建波 04.25 Spring便历Collection集合

来源:互联网 发布:物流抢单软件 编辑:程序博客网 时间:2024/06/08 06:29

示例

User.java

package www.csdn.spring.collection.set;public class User {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}


CollectionBean.java

package www.csdn.spring.collection.set;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class CollectionBean {public Set<String> sets;public void setSets(Set<String> sets) {this.sets = sets;}public CollectionBean() {super();System.out.println("=================================");}public List<User> users;public void setUsers(List<User> users) {this.users = users;}public Map<Integer,User> map;public void setMap(Map<Integer, User> map) {this.map = map;}public Properties props;public void setProps(Properties props) {this.props = props;}}


 

TestBean.java

package www.csdn.spring.collection.set;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;import java.util.Set;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean {@Testpublic void testSets(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-collection.xml");CollectionBean bean=context.getBean("collectionBean",CollectionBean.class);Set<String> sets=bean.sets;Iterator<String> it=sets.iterator();while(it.hasNext()){System.out.println(it.next());}System.out.println("----------------------List--------------");List<User> users=bean.users;for(User u:users){System.out.println(u.getName()+"------"+u.getAge());}System.out.println("----------------------map--------------");Map<Integer, User> map=bean.map;Set<Integer> setKeys=map.keySet();Iterator<Integer> itKeys=setKeys.iterator();while(itKeys.hasNext()){Integer key=itKeys.next();User user=map.get(key);System.out.println(key+":--"+user.getName()+"--"+user.getAge());}System.out.println("----------------------map2--------------");Set<Entry<Integer,User>> setentry=map.entrySet();Iterator<Entry<Integer, User>> itentry=setentry.iterator();while(itentry.hasNext()){Entry<Integer, User> entry=itentry.next();System.out.println(entry.getKey()+"----"+entry.getValue().getName()+"----"+entry.getValue().getAge());}System.out.println("----------------------props--------------");Properties props=bean.props;Set<String> setprops=props.stringPropertyNames();Iterator<String> keystr=setprops.iterator();while(keystr.hasNext()){String key=keystr.next();System.out.println(key+"----"+props.getProperty(key));}} }

spring-collection.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean"scope="singleton" lazy-init="default"><property name="sets"><set><value>哈哈哈</value><value>嘿嘿嘿</value><value>呵呵呵</value><value>ASDA</value><value>哈啊啊是是哈</value><value>哈啊三哈</value></set></property><property name="users"><array><ref bean="u1" /><ref bean="u2" /><ref bean="u3" /><ref bean="u4" /></array></property><property name="map"><map><entry key="1" value-ref="u1" /><entry key="2"><ref bean="u2" /></entry><entry key="3" value-ref="u3" /></map></property><property name="props"><props><prop key="1">jdbc:mysql</prop><prop key="2">jdbc:oracle</prop><prop key="3">jdbc:access</prop></props></property></bean><bean id="u1" class="www.csdn.spring.collection.set.User"><property name="name" value="张三" /><property name="age" value="28" /></bean><bean id="u2" class="www.csdn.spring.collection.set.User"><property name="name" value="李四" /><property name="age" value="28" /></bean><bean id="u3" class="www.csdn.spring.collection.set.User"><property name="name" value="王五" /><property name="age" value="30" /></bean><bean id="u4" class="www.csdn.spring.collection.set.User"><property name="name" value="马六" /><property name="age" value="28" /></bean></beans>