Jump to content

item.getName().startsWith("Stamina potion") - Find in order of dose


Recommended Posts

Posted

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!

Posted

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.

  • Like 1
Posted (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 by Explv
  • Like 6
Posted
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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...