如果映射值与参数匹配,我将尝试将对象添加到集合中。 map 设置为:-
Map<String, Person> people = new HashMap<>();
到目前为止我已经尝试过:-
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;
好的,现在我已经尝试过了。
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;
}
它可以编译,但不会向集合中添加任何项目。 我已经创建了 People 的实例,并将它们添加到 map 中,其中包含名称、地址和年龄类别,但仍未将任何人员添加到集合中。
最佳答案
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;
}
关于java - 添加一个映射值来设置它是否与参数匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61523260/