Jump to content

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


harrypotter

Recommended Posts

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!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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