兩個實例對象部門和僱員(Department和Employee),一個對象引用另外一個對象的集合。java
this
第一種方式:for(Entry<String, Employee> entry:de.getMap().entrySet()){ System.out.println(entry.getKey()+""+entry.getValue().getName()); };
第二種方式:spa
Iterator<String> iterator = de.getMap().keySet().iterator(); if(iterator.hasNext()){ String key = iterator.next(); System.out.println(key+""+de.getMap().get(key).getName()); }
第三種方式:code
for(String key:de.getMap().keySet()){ System.out.println(key+""+de.getMap().get(key).getName()); }
兩個類之間的關係以下:對象
public class Employee { private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } public class Department { private String name; private String[] empName; private int[] empAge; private List<Employee> employeeList; private Set<Employee> employeeSet; private Map<String,Employee> map; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getEmpName() { return empName; } public void setEmpName(String[] empName) { this.empName = empName; } public int[] getEmpAge() { return empAge; } public void setEmpAge(int[] empAge) { this.empAge = empAge; } public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } public Set<Employee> getEmployeeSet() { return employeeSet; } public void setEmployeeSet(Set<Employee> employeeSet) { this.employeeSet = employeeSet; } public Map<String, Employee> getMap() { return map; } public void setMap(Map<String, Employee> map) { this.map = map; } }