List总结:
1. 所有的List中只能容纳单个不同类型的对象组成的表,而不是Key-Value键值对。例如:[ tom,1,c ];
2. 所有的List中可以有相同的元素,例如Vector中可以有 [ tom,koo,too,koo ];
3. 所有的List中可以有null元素,例如[ tom,null,1 ];
4. 基于Array的List(Vector,ArrayList)适合查询,而LinkedList(链表)适合添加,删除操作。
Set总结:
1. Set实现的基础是Map(HashMap);
2. Set中的元素是不能重复的,如果使用add(Object obj)方法添加已经存在的对象,则会覆盖前面的对象
Map映射
映射与集或列表有明显区别,映射中每个项都是成对的。映射中存储的每个对象都有一个相关的关键字(Key)对象,关键字决定了对象在映射中的存储位置,检索对象时必须提供相应的关键字,就像在字典中查单词一样。关键字应该是唯一的。
以下是集合打出相应的部门
Department.java类
1 package com.collection; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 7 public class Department { 8 public String name ; 9 public String empName[]; 10 public List<Employ> empList; 11 public Set<Employ> empSet; 12 public Map<String,Employ> empMap; 13 public Map<String, Employ> getEmpMap() { 14 return empMap; 15 } 16 public void setEmpMap(Map<String, Employ> empMap) { 17 this.empMap = empMap; 18 } 19 public Set<Employ> getEmpSet() { 20 return empSet; 21 } 22 public void setEmpSet(Set<Employ> empSet) { 23 this.empSet = empSet; 24 } 25 public List<Employ> getEmpList() { 26 return empList; 27 } 28 public void setEmpList(List<Employ> empList) { 29 this.empList = empList; 30 } 31 public String getName() { 32 return name; 33 } 34 public void setName(String name) { 35 this.name = name; 36 } 37 public String[] getEmpName() { 38 return empName; 39 } 40 public void setEmpName(String[] empName) { 41 this.empName = empName; 42 } 43 44 }
Employ.java类
1 package com.collection; 2 3 public class Employ { 4 public String name; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 }
TestCollection.java类
1 package com.collection; 2 3 import java.util.Iterator; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 10 public class TestCollection { 11 12 /** 13 * @param args 14 */ 15 public static void main(String[] args) { 16 ApplicationContext ac = new ClassPathXmlApplicationContext("com/collection/beans.xml"); 17 Department department = (Department)ac.getBean("department"); 18 System.out.println(department.getName()); 19 for(String emp:department.getEmpName()){ 20 System.out.println(emp); 21 } 22 System.out.println(".................."); 23 for(Employ e:department.getEmpList()){ 24 25 System.out.println(e.getName()); 26 } 27 System.out.println(".................."); 28 for(Employ e:department.getEmpSet()){ 29 30 System.out.println(e.getName()); 31 } 32 33 Map<String,Employ> employee = department.getEmpMap(); 34 Iterator it = employee.keySet().iterator(); 35 while(it.hasNext()){ 36 String key = (String) it.next(); 37 Employ emp = employee.get(key); 38 System.out.println("迭代器--"+key+emp.getName()); 39 } 40 41 System.out.println(".................."); 42 for(Entry<String, Employ> entry:department.getEmpMap().entrySet()){ 43 System.out.println(entry.getKey()+" "+entry.getValue().getName()); 44 } 45 } 46 47 }
beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 7 <bean id="department" class="com.collection.Department"> 8 <property name="name" value="往事" /> 9 <property name="empName"> 10 <list> 11 <value>网络</value> 12 <value>设施</value> 13 <value>软件</value> 14 </list> 15 </property> 16 <property name="empList"> 17 <list> 18 <ref bean="emp1"/> 19 <ref bean="emp2"/> 20 </list> 21 </property> 22 <property name="empSet"> 23 <set> 24 <ref bean="emp1"/> 25 <ref bean="emp2"/> 26 </set> 27 </property> 28 <property name="empMap"> 29 <map> 30 <entry key="1" value-ref="emp1"></entry> 31 <entry key="2" value-ref="emp2"></entry> 32 </map> 33 </property> 34 </bean> 35 36 <bean id="emp1" class="com.collection.Employ"> 37 <property name="name" value="张三" /> 38 </bean> 39 <bean id="emp2" class="com.collection.Employ"> 40 <property name="name" value="李斯" /> 41 </bean> 42 </beans>