1

I have a question regarding a Hashmap that is implemented as a Family tree which looks like this. The key value of the Hashmap is the first name of the person and the value is from the class Person, which contains again the first name, the sex and an Arraylist with the children. The children are again from type person. I want to find a way to make a function, which should return an float array with two fields, in the first field the maximum of children a person has from all persons and the mean of children of all persons. Should look like this:

float[] describe(Map<String, Person> m) {
}

This is the Hashmap: (already filled with keys and values and there is also a link between them and the children)

Map<String, Person> allPersons = new HashMap<String, Person>();

This is the class Person:

class Person {
    String name;
    String sex;
    List<Person> children;

    public Person(String name, String sex) {
        this.name = name;
        this.sex = sex;
        this.children = new ArrayList<Person>();
    }
}

My question is how can u iterate through the Hashmap and also get the amount of children every person has.

AlexandraS
  • 23
  • 3
  • "maximum of children a person has from all persons" - do you care about the number of grandchildren? If yes, that would be DFS traversal. – Ervin Szilagyi Dec 25 '21 at 20:00

2 Answers2

2

It may be convenient to use Stream API and its Collectors::summarizingInt with regard to the size of children list per Person:

float[] describe(Map<String, Person> m) {
    IntSummaryStatistics stats = m.values()
        .stream() // Stream<Person>
        .collect(Collectors.summarizingInt(p -> p.getChildren().size()));

    return new float[]{ stats.getMax(), (float) stats.getAverage()};
}

If Stream API cannot be used, the stats may be computed in a simple loop:

float[] describe(Map<String, Person> m) {
    float[] stats = {0.0f, 0.0f};
    for (Person p : m.values()) {
        int kids = p.getChildren().size();
        stats[0] = Math.max(stats[0], kids);
        stats[1] += kids;
    }
    stats[1] /= Math.max(1, m.values().size());
    return stats;
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • ohh thank you so much for your quick answer! :) I am sadly not allowed to use for example the IntSummaryStatistics etc., So I found out how to iterate through the list and get the amount of the children of every person like this: (allPersons is a placeholder for the name of the hashmap): for (Map.Entry entry: allPersons.entrySet()) { System.out.println(entry.getValue().children.size()); } That gives me out the children of each person. Now I thought in saving them in a float array to fullfill the task with that afterwards.Do you now, how I can do that? Lovely greetings! – AlexandraS Dec 25 '21 at 21:59
  • @ChrisiS, you may check the update – Nowhere Man Dec 26 '21 at 00:24
1

If you don't care about the size of the grandchildren, you can use stream with summaryStatistics

Map<String, Person> m = Map.of(...);
IntSummaryStatistics stats = m.values().stream()
        .mapToInt(person -> person.children.size())
        .summaryStatistics();
float[] s = {stats.getMax(), (float) stats.getAverage()};

Iterative solution:

float max = 0, sum = 0;
for (Person person : m.values()) {
    max = Math.max(person.children.size(), max);
    sum += person.children.size();
}

float[] s = {max, sum / m.values().size()};
Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
  • ohh thank you so much for your quick answer! :) I am sadly not allowed to use for example the IntSummaryStatistics etc., So I found out how to iterate through the list and get the amount of the children of every person like this: (allPersons is a placeholder for the name of the hashmap): for (Map.Entry entry: allPersons.entrySet()) { System.out.println(entry.getValue().children.size()); } That gives me out the children of each person. Now I thought in saving them in a float array to fullfill the task with that afterwards.Do you now, how I can do that? Lovely greetings! – AlexandraS Dec 25 '21 at 22:00
  • Thank you so much! So nice of you to help out :) ! – AlexandraS Dec 26 '21 at 00:41