harrypotter Posted March 17, 2017 Posted March 17, 2017 I'm using the following as an example to find any Stamina pots in my bank: if (getBank().contains(item -> item.getName().startsWith("Stamina potion"))) { if (getBank.withdraw(item -> item.getName().startsWith("Stamina potion"), 1)) { // This always withdraws the first match, which in my case is Stamina potion(4) (I assume this is because it's the first stamina pot in my bank). } } How would I instead find the lowest dose potion first, without reordering the bank. Thanks!
Polymorphism Posted March 17, 2017 Posted March 17, 2017 Try using a comparator to re-order an array of the possible potion names. String[] staminaPots= { "stamina (4)", "stamina (3)", etc }; Arrays.asList(staminaPots).stream().sorted((s1, s2) -> s1.compareTo(s2)) //sorts the potions using natural order i believe Then withdraw the first element of the list if it's not null and bank contains, etc. 1
Explv Posted March 17, 2017 Posted March 17, 2017 (edited) 1 hour ago, harrypotter said: I'm using the following as an example to find any Stamina pots in my bank: if (getBank().contains(item -> item.getName().startsWith("Stamina potion"))) { if (getBank.withdraw(item -> item.getName().startsWith("Stamina potion"), 1)) { // This always withdraws the first match, which in my case is Stamina potion(4) (I assume this is because it's the first stamina pot in my bank). } } How would I instead find the lowest dose potion first, without reordering the bank. Thanks! You could try doing something like this if you just want to get a single potion: Optional<Item> staminaPotion = Arrays.stream(getBank().getItems()) .filter(item -> item != null && item.getName().startsWith("Stamina potion")) .min(Comparator.comparing(Item::getName)); This snippet will construct a Stream from the array of all items in the bank, filter it so that only the items which are not null and has a name beginning with "Stamina potion" remain, and then find the min of those using a Comparator to compare the Item names, this should return Stamina potion (1) first etc. Note: it returns an Optional<Item>, the Optional class can be found here You use it like so: if (staminaPotion.isPresent()) { Item pot = staminaPotion.get(); } Or to get the List of all stamina potions in the bank, sorted by dosage: List<Item> staminaPotion = Arrays.stream(getBank().getItems()) .filter(item -> item != null && item.getName().startsWith("Stamina potion")) .sorted(Comparator.comparing(Item::getName)) .collect(Collectors.toList()); This snippet is similar to the above, but instead of returning the min, it instead sorts the Stream using the same Comparator, and then collects the Items into a List Edited March 17, 2017 by Explv 6
Polymorphism Posted March 17, 2017 Posted March 17, 2017 5 minutes ago, Explv said: You could try doing something like this if you just want to get a single potion: Optional<Item> staminaPotion = Arrays.stream(getBank().getItems()) .filter(item -> item != null && item.getName().startsWith("Stamina potion")) .min(Comparator.comparing(Item::getName)); Or to get the List of all stamina potions in the bank, sorted by dosage: List<Item> staminaPotion = Arrays.stream(getBank().getItems()) .filter(item -> item != null && item.getName().startsWith("Stamina potion")) .sorted(Comparator.comparing(Item::getName)) .collect(Collectors.toList()); Dang gonna confuse the man with all that Java 8 lol.
Diclonius Posted March 17, 2017 Posted March 17, 2017 The way I'd do it is just a simple for loop, looping from 1 -> 4, for(int i = 1; i < 5; i++) { String potionName = "Stamina potion(" + i + ")"; if (getBank().contains(potionName) { getBank().withdraw(potionName); } }
Tom Posted March 17, 2017 Posted March 17, 2017 1 hour ago, Explv said: You'd make a good teacher, unlike me you can explain things in a concise and understandable way 2
Explv Posted March 17, 2017 Posted March 17, 2017 2 minutes ago, Tom said: You'd make a good teacher, unlike me you can explain things in a concise and understandable way Thanks boss