java 中的几个数组类型
1、Department类
package com.yy.collection; import java.util.List;
import java.util.Map;
import java.util.Set; public class Department {
private String name;
private String [] empName;
private List<Employee> empList;
private Set<Employee> empsets;
private Map<String,Employee> empMaps; public Map<String, Employee> getEmpMaps() {
return empMaps;
}
public void setEmpMaps(Map<String, Employee> empMaps) {
this.empMaps = empMaps;
}
public Set<Employee> getEmpsets() {
return empsets;
}
public void setEmpsets(Set<Employee> empsets) {
this.empsets = empsets;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
2.Employee类
package com.yy.collection; public class Employee {
private String name;
private int id; public String getName() {
return name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public void setName(String name) {
this.name = name;
}
}
3.beans.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 在容器文件中配置bean(service/dao/domain/action/数据源) -->
<bean id="department" class="com.yy.collection.Department">
<property name ="name" value="财务部"></property> <!-- 给list注入值 --> <property name="empName">
<list>
<value>小明</value>
<value>小小明</value>
<value>迷你明</value>
</list> </property>
<!-- 给list注入值 -->
<property name="empList">
<list>
<ref bean="emp1"/>
<ref bean="emp2"/> </list>
</property>
<!-- 给set注入值 -->
<property name="empsets">
<set>
<ref bean="emp2"/>
<ref bean="emp1"/>
</set>
</property>
<!-- 给map注入值 只要map不一样,就可以显示 -->
<property name="empMaps">
<map>
<entry key="11" value-ref="emp1" />
<entry key="22" value-ref="emp2"/>
</map>
</property>
</bean>
<bean id="emp1" class="com.yy.collection.Employee">
<property name="name" value="地址1" />
<property name="id" value="1" />
</bean>
<bean id="emp2" class="com.yy.collection.Employee">
<property name="name" value="地址2"/>
<property name="id" value="2" />
</bean>
</beans>
4.应用App1类
package com.yy.collection; import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App1 { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/yy/collection/beans.xml");
Department department=(Department) ctx.getBean("department");
System.out.println(department.getName());
for (String emName:department.getEmpName()){
System.out.println(emName); }
System.out.println("******通过list集合取出数据*****");
for(Employee array:department.getEmpList()){
System.out.println( "name="+array.getName()+""+array.getId()); }
System.out.println("******通过set集合取出数据*****");
for(Employee array:department.getEmpsets()){
System.out.println( "name="+array.getName()); } System.out.println("*****通过map集合取出数据*****");
//1.迭代器
Map<String,Employee>empmaps=department.getEmpMaps();
Iterator it=empmaps.keySet().iterator();
while(it.hasNext()){
String key=(String) it.next();
Employee emp=empmaps.get(key);
System.out.println("key="+key+""+emp.getName()); } //2、简洁方法 for( Entry<String, Employee> entry1:department.getEmpMaps().entrySet()){
System.out.println(entry1.getKey()+""+entry1.getValue().getName());
}
} }