Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

HashMap of ItemStacks?


Recommended Posts

How would I put and obtain the values within hashmap.  It always returns null?


class 1

public static HashMap<ItemStack, Integer> Levels = new HashMap(200)
ItemStack stack = new ItemStack(Blocks.stone);
Levels.put(stack, 5)



ItemStack stack = new ItemStack(Blocks.stone);
int requiredLevel = Levels.get(stack)//Is Null



Link to comment
Share on other sites


doesn't override the


method, so two instances are treated as different keys even if they share the same contents.


To use an


as a key, you'll need a wrapper class that overrides


to return the hash code of the


's contents;


will combine the hash codes of multiple objects for you.


Exactly which parts of the


you use in the hash code depends on your requirements: Do you only care about the


, stack size and metadata; or do you include the NBT as well?




to return the hash of its contents, so you don't need to calculate that yourself.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

also, if you'll only need to differentiate between item, stack size and metadata, and if you won't need to put or get things into the map too often then you could put/get ItemStack.toString. I think it would work, but it's probably slow.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

If you don't want to use a wrapper class you can also use a TCustomHashMap from the trove library (Minecraft comes with it). This lets you specifiy how to calculate equals and hashCode instead of using the normal methods.


While I will probably just use wrapper objects, that's pretty neat.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.


Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.


DO NOT PM ME WITH PROBLEMS. No help will be given.

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.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.


  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.