简体   繁体   中英

How do I iterate through a Hashtable, then add the objects to an ArrayList?

I'm making a family tree program. I've been trying to figure out how to copy objects from a Hashtable into an ArrayList (just the object Person, not the String). I have a Hashtable of children, which are Person objects. I want to check to see if the currentPerson I am working on in the program has children, and if so, add those children to an ArrayList childList. I've been working on this for a couple of hours and cannot seem to figure it out.

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;
    }

使用ArrayList addAll方法。

childList.addAll(children.get(currentPerson.getName())

Using ArrayList addAll solves your problem in a one liner as mentioned by NG.

However, if you plan on filling up the grandchildList as well then you need to change the way your are storing data. Since you are storing stuff by name, you can never really figure out if your answer is correct. The below code will work assuming your Person class has a list of children by name.

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());
    }
  }
}

Consider this case:

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

There is only one grandChild technically but your algorithm might return two depending on how you store information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM