Ekwiwalent Java c ++ equal_range (lub lower_bound i upper_bound)
Mam listę obiektów posortowanych i chcę znaleźć pierwsze wystąpienie i ostatnie wystąpienie obiektu. W C ++ mogę łatwo używać std :: equal_range (lub tylko jednego lower_bound i jednego upper_bound).
Na przykład:
bool mygreater (int i,int j) { return (i>j); }
int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;
// using default comparison:
std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
bounds=std::equal_range (v.begin(), v.end(), 20); // ^ ^
// using "mygreater" as comp:
std::sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^
std::cout << "bounds at positions " << (bounds.first - v.begin());
std::cout << " and " << (bounds.second - v.begin()) << '\n';
return 0;
}
W Javie nie ma prostej równoważności? Jak mam zrobić z równym zasięgiem
List<MyClass> myList;
Przy okazji używam standardowego importu java.util.List;