Search code examples
javarepast-simphony

"Preferences" in Java


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).


Solution

  • 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.