Reputation: 67
first, I don't know if I'm organizing my data efficiently, the idea is that I have pairs of key/value.
public static class Freq implements Comparable {
String term;
double frequency;
public Freq( String term, double frequency ) {
this.term = term;
this.frequency = frequency;
}
public int compareTo(Object o) {
if(this.frequency == ((Freq) o).frequency)
return 0;
else if(this.frequency > ((Freq) o).frequency)
return 1;
else
return -1;
}
Now, I'm storing such objects within a collection: List<Freq> bla = new ArrayList<Freq>()
as well as sorting it.
I'm interested to search for specific objects e.g. Freq.name = 'Bar'
from the collection, which is sorted. How would I do that? Or I have to iterate the whole collection.
or is there other more efficient ways to do this?
Upvotes: 1
Views: 1497
Reputation: 31
You can use JFilter http://code.google.com/p/jfilter/
JFilter is a simple and high performance open source library to query collection of Java beans.
Key features
Upvotes: 0
Reputation: 116306
You should use an associated collection such as a TreeMap
, which keeps its elements sorted automatically. If you want to search sometimes based on name and sometimes on frequency, you can keep your elements in two maps at the same time, and use the suitable one for lookup.
Or if for some reason you want to stick with a sorted List, you can use Collections.binarySearch()
to find elements in it.
Upvotes: 3