Hi, what is the most efficient (in terms of speed, and also memory cost) to update an object in a Queue/Set?
Suppose I have a Word class:
public class Word{
String word;
int frequencyInText;
}
And I want to keep Word objects in a Queue/Set. Then whenever a new Word is added to the Queue/Set, if it already exists, i want to increase the word's frequency by 1. However Queue/Set do not have a method to retrieve a particular object, in this case, this particular object is the word that already contained by the Queue/Set
I can simply iterate through every element in the Queue/Set, and check if the element equals the duplicate one. if so, modify that object. But this sounds a very heavy process when the Queue/Set is very large, also when the evaluation of equality is complex.
I dont want to use Map to keep <word, frequency> because I want words sorted according to frequency (the "value" field). Also, the collection is updated and queried every now and then. Using Map instead would be more expensive, i suppose.
Is there better ways to do this? thanks very much!