0

I'm trying to add an object to a set if the map value matches an argument. the map is set up as:-

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

so far I have tried:-

public Set<Person> findPersonInAgeCat(char ageCat) {
    Set<Person> anAge = new HashSet<>();
    for (Person eachPerson : people.values()) {
        boolean result = personSet.containsValue(ageCat);
        if (result) {
            anAge.add(eachPerson);
        }
        return anAge;

ok so, now i have tried this.

   public Set<Person> findPeopleInAgeCat(char ageCat)
{ 
    Set<Person> anAge = new HashSet<>();

     for (Person peopleValues : people.values()) 
     {
        Set<Person> values = new HashSet<>();
        values.add(peopleValues);
        for (Person info : values)
        {  
           if (values.contains(ageCat))
           {
              anAge.add(info);
           }
        }

     }
   return anAge;

}

Which compiles but doesn't add any items to the set. I have created the instances of People and they are added to the Map with names, address, and age category, but still not adding any people to the set.

  • [What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – mplungjan Apr 30 '20 at 12:23
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Apr 30 '20 at 12:24
  • Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. What happens when you execute the code you posted (which is not complete) – mplungjan Apr 30 '20 at 12:24

1 Answers1

0
  public Set<Person> findPeopleInAgeCat(char ageCat)

{
    Set<Person> anAge = new HashSet<>();
    Set<Person> values = new HashSet<>();

    for (Person eachPerson : people.values())   
    {  
       char anAgeCat = eachPerson.getAgeCategory();
        if (anAgeCat == ageCat)
        {
           anAge.add(eachPerson);
        }
    }

    return anAge;
 }