0

I don't know exactly how to word this, but I am wondering if it is possible to have "preferences" when choosing from a list.

For example, if I have a list of comparable tree objects with different variables (size, type, height), is it possible to "prefer" say bigger tree/ one type of tree over the other etc. In other words when choosing one object from the list, the tendency is to choose the largest tree or to choose one type of tree over the other, but not always (there is a stochastic element).

Taylor Marie
  • 357
  • 1
  • 15
  • Why not use something like a Binary min/max Heap? Or even a priority queue implemented as a tree? These use specific representations to choose preference or priority. – Evan Bechtol Mar 27 '15 at 19:18
  • Alternatively you could use specific conditions to compare and decide: if (tree1 > tree2 && tree1.height > tree2.height) {Do something} – Evan Bechtol Mar 27 '15 at 19:20

1 Answers1

3

The idea you want is a sorted list. Various methods exist for this such as Collections.sort and Arrays.sort, as well as several java.util classes that automatically sort themselves (consider also some structures other than lists, like heaps). You can either make Trees Comparable<Tree> or make a Comparator<Tree> so that you can create lists arranged so that you can easily find the "most preferable" item (which will be moved to the front of the list) or the "least preferable" item (at the end of the list). See the documentation for the various classes I have mentioned for how to accomplish this.

Vitruvie
  • 2,327
  • 18
  • 25
  • I understand that, but I also don't ALWAYS want to select the most preferred tree. – Taylor Marie Mar 27 '15 at 19:52
  • @TaylorMarie If you want the 2nd most preferred tree, you can just pick the 2nd item, and so on. All the items are there, just arranged to find them based on preferredness. – Vitruvie Mar 27 '15 at 19:52