Reputation: 11
i have this code:
public class Game {
String NO_MOVEMENTS = ChatColor.RED+"You don't have sufficient movements to do that";
public static HashMap<Player,Integer> movements = new HashMap<>();
public void setupMovements(Player player){
movements.put(player,15);
}
public void movementsManager(Player player){
}
public static HashMap arrowShoow(Player player){
}
}
And i need to know the "movements"(int) of the hashmap, how i do get it?? thank you so much!
Upvotes: 0
Views: 334
Reputation: 18923
The Order (of entry) is not preserved in HashMap, unlike ArrayList. And there is no way you can get the so called second
object in HashMap.
To access all the elements in a HashMap you can look at this answer.
To get the movement of a particular player you can use:
Integer movement = movements.get(yourPlayerObject);
Upvotes: 2
Reputation: 11832
If you want to know the Integer
value for a specified Player
:
Integer value = movements.get(player);
If you want to know the Player
for a specified Integer
:
Integer value = 15; /* put the int value you are looking for here */
Player player = movements.entrySet().stream()
.filter(entry -> entry.getValue().equals(value));
Keep in mind that if there is no Player
for a specific Integer
, you will get a null.
If you want the second object inserted into the map, you will need to use a LinkedHashMap
, which retains the order in which keys were inserted into the map. To retrieve the second item inserted into the map:
Player player = movements.keySet().get(1);
Note: this assumes that there are at least two items in the map. It can also change if you remove an item from the map (because if you remove the first item from the map, the second item will become the first).
Upvotes: 0