Use a NavigableMap
, which will allow you to retrieve the correct outcome with one clean and simple lookup. (And internally, this uses an efficient O(log n) lookup—not that your maps will be large enough to matter.)
import java.util.NavigableMap;
import java.util.TreeMap;
import static java.util.concurrent.ThreadLocalRandom.current;
final class LoadedDie {
public static void main(String... argv) {
/* One-time setup */
NavigableMap<Integer, String> loot = new TreeMap<>();
int cumulative = 0;
loot.put(cumulative += 20, "Gold");
loot.put(cumulative += 30, "Iron");
loot.put(cumulative += 50, "Coal");
/* Repeated use */
System.out.println(loot.higherEntry(current().nextInt(cumulative)).getValue());
System.out.println(loot.higherEntry(current().nextInt(cumulative)).getValue());
}
}