两个实例对象部门和雇员(Department和Employee),一个对象引用另一个对象的集合。
第一种方式:
for(Entryentry:de.getMap().entrySet()){ System.out.println(entry.getKey()+""+entry.getValue().getName()); };
第二种方式:
Iteratoriterator = de.getMap().keySet().iterator();if(iterator.hasNext()){ String key = iterator.next(); System.out.println(key+""+de.getMap().get(key).getName()); }
第三种方式:
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 ListemployeeList; private Set employeeSet; private Map 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 getEmployeeList() { return employeeList; } public void setEmployeeList(List employeeList) { this.employeeList = employeeList; } public Set getEmployeeSet() { return employeeSet; } public void setEmployeeSet(Set employeeSet) { this.employeeSet = employeeSet; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; }}