-4

Let's assume that I have a list of Word objects. Each Word has the following structure preliminary:

class Word {
    String text;
    double rank;
}

How can I implement the solution to show the user random word from list by relevancy? User, for example, can adjust rank field to force the word appear more frequently or set the rank to 0 in order to make it not showable at all.

Thank you!

UPD Will PriorityQueue be ok for that?

sidlejinks
  • 709
  • 1
  • 9
  • 25

1 Answers1

0

I dont know exactly how much you want to weight chances by rank, but the solution I would suggest would be to create an array of Word Objects with a number of instances of each Word Object equal to its rank. So if the rank was zero the array would contain none of them and it would never be picked, or if the rank was 10 the array would contain 10 instances of that Object and it would have a higher chance to be picked. I dont know if this is exactly what you're looking for, but a similar idea could probably work.

  • That's good idea. But I don't think multiplying the quantity of objects in memory by rank is cheap and clean solution. Thank you! – sidlejinks Jul 16 '15 at 14:02