Java 如何遍历哈希表,然后将对象添加到ArrayList?

Java 如何遍历哈希表,然后将对象添加到ArrayList?,java,Java,我正在制作一个家谱节目。我一直在试图弄清楚如何将对象从哈希表复制到ArrayList(只复制对象Person,而不是字符串)。我有一个子对象的哈希表,它们是Person对象。我想检查我在程序中工作的当前人员是否有子项,如果有,请将这些子项添加到ArrayList子项列表中。我已经在这方面工作了几个小时,但似乎还没弄明白 import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; public

我正在制作一个家谱节目。我一直在试图弄清楚如何将对象从哈希表复制到ArrayList(只复制对象Person,而不是字符串)。我有一个子对象的哈希表,它们是Person对象。我想检查我在程序中工作的当前人员是否有子项,如果有,请将这些子项添加到ArrayList子项列表中。我已经在这方面工作了几个小时,但似乎还没弄明白

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;

public class FamilyInfo {
//////////// instance variables
// hash table that tells the father of a named person, if known
private Hashtable<String, Person> fathers;
// hash table that tells the mother of a named person, if known
private Hashtable<String, Person> mothers;
// hash table that tells the children of a named person, if any are known.
// In theory the father, mother and children tables should be kept consistent
private Hashtable<String, HashSet<Person>> children;


/**
 * constructor -- initializes instance variables
 */
public FamilyInfo() {
    // initialize everything to be empty collections of the appropriate type
    fathers = new Hashtable<String, Person>();
    mothers = new Hashtable<String, Person>();
    children = new Hashtable<String, HashSet<Person>>();
}

    public ArrayList<String> grandchildNames(Person currentPerson) {
    // return a dummied up name telling that the method is not implemented
    ArrayList<String> rtnVal = new ArrayList<String>();

    //Create an ArrayList that will hold the child 
    ArrayList<Person> childList = new ArrayList<Person>();

    //Create an ArrayList that will hold the granchildren
    ArrayList<Person> grandchildList = new ArrayList<Person>();

    //Add children to the child list
    if(children.get(currentPerson.getName()) != null)
    {
       //add the children to childList from the children hashtable
    }


    return rtnVal;
    }
import java.util.ArrayList;
导入java.util.HashSet;
导入java.util.Hashtable;
公共类家庭信息{
////////////实例变量
//一个哈希表,它告诉一个已知姓名的人的父亲
私人哈希表父亲;
//一个哈希表,它告诉一个已知姓名的人的母亲
私人哈希表母亲;
//一个哈希表,它告诉一个已命名的人(如果有)的孩子。
//理论上,父亲、母亲和孩子的桌子应该保持一致
私有哈希表儿童;
/**
*构造函数——初始化实例变量
*/
公共家庭信息(){
//将所有内容初始化为适当类型的空集合
父亲=新哈希表();
母亲=新哈希表();
children=新哈希表();
}
公共数组列表名称(Person currentPerson){
//返回一个虚拟名称,告知该方法未实现
ArrayList rtnVal=新的ArrayList();
//创建一个将容纳子对象的ArrayList
ArrayList childList=新建ArrayList();
//创建一个ArrayList,它将保存子项
ArrayList=新的ArrayList();
//将子项添加到子项列表中
if(children.get(currentPerson.getName())!=null)
{
//将子项从子项哈希表添加到子项列表
}
返回rtnVal;
}
使用该方法


如NG所述,使用ArrayList addAll可以在一行程序中解决您的问题

但是,如果您计划同时填充子类列表,则需要更改存储数据的方式。因为您是按名称存储数据的,所以您永远无法确定您的答案是否正确。假设您的Person类有一个按名称存储的子类列表,下面的代码将起作用

if (children.containsKey(currentPerson.getName()) {
  for (Person c : children.get(currentPerson.getName())) {
    childList.add(c);
    //Now assuming Person class keeps a list of children names
    if (c.getChildren() != null) {
        grandchildList.addAll(c.getChildren());
    }
  }
}

考虑这种情况:

[Root]->[Parent-Bob]->[Child-Sam]->[grant-Peter]
[Root]->[Parent-Sam]->[Child-Bob]


从技术上讲,只有一个孙子,但您的算法可能会返回两个孙子,具体取决于您存储信息的方式。

childList.addAll(children.get(currentPerson.getName())
使用HashMap而不是Hashtable(javadoc:
从Java 2平台v1.2开始,该类经过改装以实现Map接口,使其成为Java集合框架的成员。与新集合实现不同,哈希表是同步的。如果不需要线程安全实现,建议使用HashMap代替Hashtable。如果线程如果希望实现高度并发,则建议使用ConcurrentHashMap代替Hashtable。
)。
if (children.containsKey(currentPerson.getName()) {
  for (Person c : children.get(currentPerson.getName())) {
    childList.add(c);
    //Now assuming Person class keeps a list of children names
    if (c.getChildren() != null) {
        grandchildList.addAll(c.getChildren());
    }
  }
}