We have an object that looks like a "recursive" data structure.
Suppose we have a Person object whose structure is similar to this
public class Person {
private String id;
private Map<String,Person> persons;
public Person(String id, Map<String,Person> persons){
this.id = id;
this.persons = persons;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, Person> getPersons() {
return persons;
}
public void setPersons(Map<String, Person> persons) {
this.persons = persons;
}
@Override
public String toString() {
return "Person [id=" + id + ", persons=" + persons + "]";
}
}
An approximate representation of this object will be: (sample data)
Person p1 = new Person("Jim", ImmutableMap.of("A001",new Person("Mike",ImmutableMap.of("D001",new Person("Jack",ImmutableMap.of("E001",new Person("Kim",null))))),
"Z001",new Person("Adam",ImmutableMap.of("Y001",new Person("Eve",ImmutableMap.of("X001",new Person("Dave",null)))))));
Note 1: ImmutableMapfrom the google guava collection
Note 2: Assume that the “key” for the Card in the Person object is the name of the person.
Given a person’s name, what is the most efficient way to go through an iteration and get an identifier. ?
For example, if the input is "Eve", the output should be "Y001"