In Java, is there something similar to C ++ standard library listings that use a comparator to search for a specific object in the list of one of its variables?
For example, instead of iterating over an ArrayList looking for a specific object, checking for a comparison of variables. Is there a way to use a comparator object to search for a specific instance?
(Note: I do not want to use hashmap, as this would create two separate lists. I want the functionality of the list not to be associated with the use of hashmap.)
Something like this, but for Java:
#include <algorithm>
using namespace std;
class Cperson
{
string lastname, firstname, address, city;
int zipcode;
char state[3];
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;
}
Now we can search our private list in the firstname field, ignoring the other fields:
vector<Cperson> personlist;
Cperson searchFor;
vector<Cperson>::iterator fperson;
fperson= std::find(personlist.begin(),
personlist.end(),
searchFor,
firstEqualTo);
source
share