In Java is there something similar to C++ Standard Library lists that uses a comparator to find a specific object in a list by one of its variables?
For instance instead of looping through an ArrayList looking for a specific Object by checking for the variable comparison. Is there a way to use a comparator object to find a specific instance?
(Note: I don't want to use a hashmap as that would create two seperate lists. I want the functionality of a list without having to have a hashmap involved.)
Something like this, but for Java:
#include <algorithm>
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;
}
Now we can search our personlist on the firstname field, ignoring the other fields:
vector<Cperson> personlist;
// fill personlist somehow
Cperson searchFor; // should contain the firstname we want to find
vector<Cperson>::iterator fperson;
fperson= std::find(personlist.begin(),
personlist.end(),
searchFor,
firstEqualTo);