Reputation: 19
I am trying to store a temporary value with it's properties like shows in the example:
String -> IN_LOBBY -> true
IN_MID_GAME -> false
IN_DEATHMATCH -> false
I made a hashmap like this but idk how to use it:
HashMap<String, HashMap<String, Boolean>> games = new HashMap<>();
The function's property is kinda like HashMap. Idk how to do it I need help.
Upvotes: 0
Views: 1383
Reputation: 66
You can use Map
's put
and get
methods to add or retrieve keys and values.
HashMap<String, HashMap<String, Boolean>> games = new HashMap<>();
HashMap<String, Boolean> values = new HashMap<>();
values.put("IN_LOBBY", true);
values.put("IN_MID_GAME", false);
values.put("IN_DEATHMATCH", false);
games.put("String", values);
System.out.println(games.get("String").get("IN_MID_GAME"));
Upvotes: 0
Reputation: 338654
Map
You can define your map using just Map
, if you do not need to access any methods specific to HashMap
. Doing so affords you the luxury of being able to change your choice of specific Map
implementation without breaking calling code.
Map < String, Map < SportMode, Boolean > > games = new HashMap <>();
From the looks of your example, the IN_LOBBY
etc. values could be defined as an enum. An enum is appropriate when you have a limited number of possible values all known at compile-time. Using an enum provides type-safety, ensures valid values, and makes your code more self-documenting. See tutorial by Oracle.
package work.basil.example;
public enum SportMode
{
IN_LOBBY , IN_MID_GAME , IN_DEATHMATCH
}
If you want non-modifiable maps nested, use Map.of
found in Java 9 and later.
The Map.of
method provides for simple literal syntax, passing a series of key-value pairs. In our case, the pairs are string , map , string , map , ….
games.put(
"Alice" ,
Map.of( SportMode.IN_LOBBY , Boolean.TRUE , SportMode.IN_MID_GAME , Boolean.FALSE , SportMode.IN_DEATHMATCH , Boolean.FALSE )
);
games.put( "Bob" , Map.of( SportMode.IN_LOBBY , Boolean.TRUE , SportMode.IN_MID_GAME , Boolean.TRUE , SportMode.IN_DEATHMATCH , Boolean.FALSE ) );
games.put( "Carol" , Map.of( SportMode.IN_LOBBY , Boolean.TRUE , SportMode.IN_MID_GAME , Boolean.TRUE , SportMode.IN_DEATHMATCH , Boolean.TRUE ) );
Dump to console.
System.out.println( "games.toString(): " + games );
games.toString(): {Bob={IN_DEATHMATCH=false, IN_LOBBY=true, IN_MID_GAME=true}, Alice={IN_DEATHMATCH=false, IN_LOBBY=true, IN_MID_GAME=false}, Carol={IN_DEATHMATCH=true, IN_LOBBY=true, IN_MID_GAME=true}}
If your nested maps must be modifiable, I would still use Map.of
for its handy literal syntax, but feed it to the constructor of another Map
implementation. In our case, the keys of the nested map are defined in an enum, so use EnumMap
as it is highly-optimized for fast speed and low memory usage, and keeps its keys in enum-defined order.
Map < String, Map < SportMode, Boolean > > games = new HashMap <>();
games.put(
"Alice" ,
new EnumMap <>(
Map.of( SportMode.IN_LOBBY , Boolean.TRUE , SportMode.IN_MID_GAME , Boolean.FALSE , SportMode.IN_DEATHMATCH , Boolean.FALSE )
)
);
games.put( "Bob" , new EnumMap <>( Map.of( SportMode.IN_LOBBY , Boolean.TRUE , SportMode.IN_MID_GAME , Boolean.TRUE , SportMode.IN_DEATHMATCH , Boolean.FALSE ) ) );
games.put( "Carol" , new EnumMap <>( Map.of( SportMode.IN_LOBBY , Boolean.TRUE , SportMode.IN_MID_GAME , Boolean.TRUE , SportMode.IN_DEATHMATCH , Boolean.TRUE ) ) );
games.toString(): {Bob={IN_LOBBY=true, IN_MID_GAME=true, IN_DEATHMATCH=false}, Alice={IN_LOBBY=true, IN_MID_GAME=false, IN_DEATHMATCH=false}, Carol={IN_LOBBY=true, IN_MID_GAME=true, IN_DEATHMATCH=true}}
Modify the 3rd element of the first nested map.
games.get( "Alice" ).replace( SportMode.IN_DEATHMATCH , Boolean.TRUE );
games.toString(): {Bob={IN_LOBBY=true, IN_MID_GAME=true, IN_DEATHMATCH=false}, Alice={IN_LOBBY=true, IN_MID_GAME=false, IN_DEATHMATCH=true}, Carol={IN_LOBBY=true, IN_MID_GAME=true, IN_DEATHMATCH=true}}
Map
implementationsIf you want to keep your Alice
, Bob
, and Carol
keys in sorted order, use a SortedMap
/NavigableMap
implementation such as TreeMap
for the outer map.
If you want to preserve the order in which keys were added to the outer map, use LinkedHashMap
.
Upvotes: 3
Reputation: 3187
You can use it like this:
games.put("string", new HashMap<String, Boolean>());
games.get("string").add("In_lobby", true); // here we get the hashmap that corresponds do string and we add "in_lobby" to it
//... etc
Upvotes: 0