Psvxe Posted May 1, 2015 Share Posted May 1, 2015 I've tried a few things but none of them do exactly what I want it to do. I don't want to write up all kind of food but instead I want to figure out what they use as food by checking their inventory. Now the problem is that they coud have different items beside food too in their inventory. This is why I came up with the idea to filter the items by the menu action they have. However, the API doesn't contain a method for that. So now I'm working on my own method. This is my concept idea; Get all items in inventory and put them in a arrayList Figure out if items in inventory has the action "Eat" If not, remove from arrayList. Now I'm just not sure how I'm going to create this. Any help is welcome! Quote Link to comment Share on other sites More sharing options...
Flamezzz Posted May 1, 2015 Share Posted May 1, 2015 inventory.filter(new Filter<Item>() { public boolean match(Item item) { return item.hasAction("Eat"); } }); Something like this? Quote Link to comment Share on other sites More sharing options...
Alek Posted May 1, 2015 Share Posted May 1, 2015 List<Item> items = Arrays.asList(getInventory().getItems()).stream().filter(s -> s.hasAction("Eat")).collect(Collectors.toList()); Quote Link to comment Share on other sites More sharing options...
Psvxe Posted May 1, 2015 Author Share Posted May 1, 2015 inventory.filter(new Filter<Item>() { public boolean match(Item item) { return item.hasAction("Eat"); } }); Something like this? List<Item> items = Arrays.asList(getInventory().getItems()).stream().filter(s -> s.hasAction("Eat")).collect(Collectors.toList()); I'll try both of them. Looks promising. And way smoother than what I did.. haha Thanks! Quote Link to comment Share on other sites More sharing options...
Flamezzz Posted May 1, 2015 Share Posted May 1, 2015 I'll try both of them. Looks promising. And way smoother than what I did.. haha Thanks! An even nicer and actually readable solution would be (if you want to use java 8 with lambda expressions): List<Item> food = inventory.filter( item -> item.hasAction("Eat") ); Quote Link to comment Share on other sites More sharing options...
Fruity Posted May 1, 2015 Share Posted May 1, 2015 (edited) This is where i need to learn better methods all the ones above look better than what i would have done lel. I would have gotten a all the items in inventory then for each item check if it has the action and if it does add it to a array list >.> My way seems t0o much - Im such a nub atm aha Edited May 1, 2015 by Fruity Quote Link to comment Share on other sites More sharing options...
Botre Posted May 1, 2015 Share Posted May 1, 2015 This is where i need to learn better methods all the ones above look better than what i would have done lel. I would have gotten a all the items in inventory then for each item check if it has the action and if it does add it to a array list >.> My way seems t0o much - Im such a nub atm aha The methods above only became accessible to us a couple of weeks ago so dw, yours was pretty much the way to go before that ;) Quote Link to comment Share on other sites More sharing options...
Psvxe Posted May 1, 2015 Author Share Posted May 1, 2015 An even nicer and actually readable solution would be (if you want to use java 8 with lambda expressions): List<Item> food = inventory.filter( item -> item.hasAction("Eat") ); I'll get look up on lambda first. Didn't had time to study that. Quote Link to comment Share on other sites More sharing options...
Joseph Posted May 1, 2015 Share Posted May 1, 2015 getInventory().filter(new ActionFliter<Item>(actions)); This will return a list of items with the action. Actions I think is a varg aka array Quote Link to comment Share on other sites More sharing options...
Botre Posted May 1, 2015 Share Posted May 1, 2015 getInventory().filter(new ActionFliter<Item>(actions)); This will return a list of items with the action. Actions I think is a varg aka array You should probably move on and start using the Java filter :x Quote Link to comment Share on other sites More sharing options...
Fruity Posted May 1, 2015 Share Posted May 1, 2015 The methods above only became accessible to us a couple of weeks ago so dw, yours was pretty much the way to go before that Ah right made me feel better now ;) much loves Quote Link to comment Share on other sites More sharing options...