Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

update particular elements in a queue/set

807606Feb 19 2007 — edited Feb 20 2007
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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 20 2007
Added on Feb 19 2007
6 comments
436 views