在Java中,是否有类似于C标准库列表的内容,该列表使用比较器通过其变量之一在列表中查找特定对象?
例如,而不是通过检查变量比较而不是遍历ArrayList来查找特定对象.有没有一种方法可以使用比较器对象查找特定实例?
(注意:我不想使用哈希图,因为那样会创建两个单独的列表.我想要列表的功能而不必涉及哈希图.)
像这样,但对于Java:
#include
using namespace std;
class Cperson
{
string lastname, firstname, address, city;
int zipcode;
char state[3];
// this works for the last name
friend bool operator==(const Cperson& left, const Cperson& right);
friend bool firstEqualTo(const Cperson& left, const Cperson& right);
};
bool operator==(const Cperson& left, const Cperson& right)
{
return left.lastname == right.lastname;
}
bool firstEqualTo(const Cperson& left, const Cperson& right)
{
return left.firstname == right.firstname;
}
现在我们可以在名字字段上搜索我们的人员列表,而忽略其他字段:
vector personlist;
// fill personlist somehow
Cperson searchFor; // should contain the firstname we want to find
vector::iterator fperson;
fperson= std::find(personlist.begin(),
personlist.end(),
searchFor,
firstEqualTo);