Reputation: 14490
I'm trying to improve my game's rendering by bulk rendering the textures of blocks of the same type.
Each block in my game is defined by a class in the format BlockWood, which all extend from the class Block.
I currently have a map between the different block classes/types, and an ArrayList which contains the positions of all the blocks of that type, on the map.
HashMap<Block, ArrayList<Vector2f>> blockMap = new HashMap<Block, ArrayList<Vector2f>>();
Using this, I can loop through the map, bulk rendering each type of block, speeding up my rendering.
This approach however, doesn't work. I want to be able to access the ArrayList's using a dummy class as so -
blockMap.get(BlockWood.class);
Is there any efficient way I can map a class type to an ArrayList? Or will I just have to map the class string representations?
Upvotes: 0
Views: 164
Reputation: 10241
The problem here is that Map has two values (a key and value), while a List only has one value (an element).
Therefore, you can either get a list of all keys or list of all values.
for getting list of all keys, you can do
ArrayList<Block> keys = new ArrayList<Block>(blockMap.keySet());
for getting list of all Values, you can do
ArrayList<ArrayList<Vector2f>> values = new ArrayList<ArrayList<Vector2f>>(blockMap.values());
Upvotes: -1
Reputation: 17321
You want to use Class
as the key type. Your current implementation is using a Block
instance as the key. Where as using Class
will use the type of Block
as the key.
Upvotes: 1
Reputation: 115328
Your map is not between class and list. It maps instance of Block
to list. So, you always have to use instance of Block to access its lists.
blockMap.get(new BlockWood());
The question is just what will you get? You will get list that was previously stored in this map using this key.
BTW to improve your code change your map definition to:
Map<Block, List<Vector2f>> blockMap = new HashMap<Block, List<Vector2f>>();
Now you can use any type of list.
Upvotes: 1