spring入门(5)-----spring中遍历各种集合 (四)

2014-11-24 11:14:46 · 作者: · 浏览: 2
+"-----"+props.getProperty(key));
}
}
}

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 sets=bean.sets;
//得到迭代器
Iterator it=sets.iterator();
//遍历
while (it.hasNext()){
System.out.println(it.next());
}


System.out.println("--------------list----------------");
List users = bean.users;
for (User u : users) {
System.out.println(u.getName() + "-------------" + u.getAge());
}

System.out.println("----------------map1---------------");
//map1
Map map=bean.map;
//得到map集合的key键值的set集合
Set setkeys=map.keySet();
//得到key键值set集合的迭代器
Iterator 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> setentry=map.entrySet();
//获取实体对象的迭代器
Iterator> itentry =setentry.iterator();
//迭代
while(itentry.hasNext()){
//得到具体的Entry对象
Entry 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 setprops = props.stringPropertyNames();
//String集合迭代器
Iterator 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---