Java – deletes the last known entry in HashMap on map

OK, so this is a different bit I have a new HashMap

private Map<String,Player> players = new HashMap<String,Player>();

How do I delete the last known item from? Maybe someone like this?

hey = Player.get(players.size() - 1);
Player.remove(hey);

Solution

I'm a little confused First, you say you have a new ArrayList. You use a line to create a new HashMap Secondly, does the player class really have static methods such as get (int) and remove (object)?

HashMap has no specific order, ArrayList (like any other list)

Remove from ArrayList

If you have a player list, you can do the following:

private List<Player> players = new ArrayList<Player>();
// Populate the list of players
players.remove(players.size() - 1);

Here, I use the remove (int) method of list, which allows the deletion of items at any index

Delete from HashMap

If you have a player map, there is no "last item" Of course, you can iterate the map, and one of the items will pop up at the end, but that doesn't mean anything Therefore, you must first find out what to delete You can then do the following:

private Map<String,Player>();
// Populate the map of players
// Find the key of the player to remove
players.remove(toRemove);

Here, I use the remove (object) method of map Note that to delete some key value pairs, you must display the key instead of the value

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>