程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> spring入門(5) spring中遍歷各種集合

spring入門(5) spring中遍歷各種集合

編輯:關於JAVA

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

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

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

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

1-----jdbc:oracle

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved