问题
In C++ we can use __gnu_pbds
i.e. Policy based data structure. See the following code.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> PBDS;
int main()
{
PBDS st;
st.insert(1);
st.insert(3);
st.insert(4);
st.insert(10);
st.insert(15);
for(int i=0;i<st.size();i++)
{
cout<<i<<" "<< *st.find_by_order(i) <<endl;
}
cout<<st.order_of_key(5)<<endl; // 3
}
It has two important functionalities.
find_by_order(k)
: which returns an iterator so we can access it's index in PBDS set.order_of_key(k)
: which return number of elements less than k.
Both the functions runs in O(logn)
time. I want to use them in java. As these functions runs in logarithmic time Java TreeSet
would be best match. for find_by_order(k)
I have to convert TreeSet
into a List
to access its index. In TreeSet
there are methods like higher()
and lower()
which return at least greater / at least smaller element. But I want the count of numbers less than k. So is it possible to do it in java ?
Help me figure it out. If I'm wrong at some point of time then let me know. Is their way to do it using java 8 streams
? orElse using Collection Framework ?
thanks✌️
来源:https://stackoverflow.com/questions/62356532/is-there-a-java-function-similar-to-cs-pbds-order-of-key