OK so this is a BIT different. I have a new HashMap
private Map<String, Player> players = new HashMap<String, Player>();
How do I remove last known item from that? Maybe somethign like this?
hey = Player.get(players.size() - 1);
Player.remove(hey);
I’m a little bit confused. First of all, you’re saying that you’ve got a new
ArrayListand you’re illustrating this with a line that creates a newHashMap. Secondly, does thePlayerclass really have static methods likeget(int)andremove(Object)?HashMapdoesn’t have a particular order,ArrayList(as any otherList) does.Removing from an
ArrayListIf you’ve got a list of players, then you can do the following:
Here, I’ve used the
remove(int)method ofList, which allows to remove an item at an arbitrary index.Removing from a
HashMapIf you’ve got a map of players, there’s no such thing as “the last item”. Sure, you can iterate over the map and one of the items will pop out last, but that doesn’t mean anything. Therefore, first you have to find out what you want to remove. Then you can do the following:
Here, I’ve used the
remove(Object)method ofMap. Note that in order to remove some key-value pair, you have to show the key, not the value.