您的位置:首页 > 编程语言 > Java开发

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

2015-11-03 22:59 489 查看
spring-collection.xml

[html] view
plaincopy

<?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">  

    <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->  

    <bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean" scope="singleton" lazy-init="default">  

        <!-- set集合 -->  

        <property name="sets">  

            <set>  

                <value>岑红军</value>  

                <value>军哥</value>  

                <value>哈哈</value>  

                <value>呵呵</value>  

                <value>嘿嘿</value>  

                <value>洗洗</value>  

            </set>  

        </property>  

    <!-- list集合 -->     

    <property name="users">  

        <!-- <list>  

            <ref bean="u1"/>  

            <ref bean="u2"/>  

            <ref bean="u3"/>  

            <ref bean="u4"/>  

        </list> -->  

        <array>  

            <ref bean="u1"/>  

            <ref bean="u2"/>  

            <ref bean="u3"/>  

            <ref bean="u4"/>  

        </array>  

    </property>  

    <!-- map -->  

    <property name="map">  

        <map>  

            <entry key="1" value-ref="u1"></entry>  

            <entry key="2">  

                <ref bean="u2"/>  

            </entry>  

            <entry key="3" value-ref="u3">  

            </entry>  

          

        </map>  

    </property>  

      

    <property name="props">  

        <props>  

            <prop key="1">jdbc:oracle</prop>  

            <prop key="2">jdbc:mysql</prop>  

            <prop key="3">jdbc:access</prop>  

        </props>  

    </property>  

    </bean>  

    <bean id="u1" class="www.csdn.spring.collection.set.User">  

        <property name="name" value="deep" />  

        <property name="age" value="21" />  

    </bean>  

    <bean id="u2" class="www.csdn.spring.collection.set.User">  

        <property name="name" value="deepsoul" />  

        <property name="age" value="22"></property>  

    </bean>  

    <bean id="u3" class="www.csdn.spring.collection.set.User">  

        <property name="name" value="chrp" />  

        <property name="age" value="23" />  

    </bean>  

    <bean id="u4" class="www.csdn.spring.collection.set.User">  

        <property name="name" value="redarmy" />  

        <property name="age" value="28" />  

    </bean>  

      

      

  

</beans>  

User.java

[java] view
plaincopy

package www.csdn.spring.collection.set;  

  

public class User {  

    private String name;  

    private Integer age;  

    public void setName(String name) {  

        this.name = name;  

    }  

    public void setAge(Integer age) {  

        this.age = age;  

    }  

      

    public String getName() {  

        return name;  

    }  

    public Integer getAge() {  

        return age;  

    }  

    @Override  

    public String toString() {  

        return "User [name=" + name + ", age=" + age + "]";  

    }  

      

      

}  

Collection.java

[java] view
plaincopy

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 {  

    //set集合  

        public Set<String> sets;  

  

        public void setSets(Set<String> sets) {  

            this.sets = sets;  

        }  

          

        public CollectionBean() {  

            super();  

            System.out.println("========================");  

        }  

          

        //list集合  

        public List<User> users;  

        public void setUsers(List<User> users) {  

            this.users = users;  

        }  

          

        //map集合  

        public Map<Integer,User> map;  

  

        public void setMap(Map<Integer, User> map) {  

            this.map = map;  

        }  

          

        //props集合  

        public Properties props;  

  

        public void setProps(Properties props) {  

            this.props = props;  

        }  

          

}  

TestBean.java

[java] view
plaincopy

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 {  

      

    @Test  

    public void tests(){  

        ApplicationContext context = new ClassPathXmlApplicationContext(  

                "classpath:spring-collection.xml");  

        CollectionBean bean = context.getBean("collectionBean",  

                CollectionBean.class);  

        System.out.println("---------------set----------------");  

        //获取set集合  

        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("----------------map1---------------");  

        //map1  

        Map<Integer,User> map=bean.map;  

        //得到map集合的key键值的set集合  

        Set<Integer> setkeys=map.keySet();  

        //得到key键值set集合的迭代器  

        Iterator<Integer> itkeys=setkeys.iterator();  

        //迭代键值  

        while(itkeys.hasNext()){  

            //得到一个具体的键值  

            Integer key = itkeys.next();  

            //通过map集合的get(key)方法 获取key键值对应的value值  

            User user =map.get(key);  

            System.out.println(key+"------"+user.getName()+"-----"+"------"+user.getAge());  

        }  

          

        System.out.println("--------------map2--------------");  

        //map2  

        //获取实体对象的set集合  

        Set<Entry<Integer,User>> setentry=map.entrySet();  

        //获取实体对象的迭代器  

        Iterator<Entry<Integer, User>> itentry =setentry.iterator();  

        //迭代  

        while(itentry.hasNext()){  

            //得到具体的Entry对象  

            Entry<Integer,User> entry=itentry.next();  

            //通过entry对象的getKey()和getValue得到  

            System.out.println(entry.getKey()+"----"+entry.getValue().getName()+"------"+entry.getValue().getAge());  

        }  

          

          

        System.out.println("--------------props-------------");  

        Properties props = bean.props;  

          

        //得到这个结合键值的key的set集合  

        Set<String> setprops = props.stringPropertyNames();  

        //String集合迭代器  

        Iterator<String> keystr = setprops.iterator();  

          

        while(keystr.hasNext()){  

            //具体键值  

            String key = keystr.next();  

            //getProperty(key)获取key对应的value值  

            System.out.println(key+"-----"+props.getProperty(key));  

        }  

    }  

}  

控制台输出:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: