user12314100
user12314100

Reputation:

Java complex maps?

I´m sorry but I can´t think of other titles for my question. I´m writing a programm in Java, with a big amount of diffrent variables. So I decided to use HashMaps (or in generel maps). But I dont know that much about complex maps in Java. I only know "HashMaps" and how to use them. Here are 2 diffrent HashMaps which I thougth I can use :

public class Storage {
public static HashMap<String, Boolean> user = new HashMap<>();
public static HashMap<Integer, Double> money = new HashMap<>();
}

So, now it´s getting a bit confusing. So, as you can see, there are 2 HashMaps. In the first HashMap is the Value to every key true or false.

public class test {
String s = "Tester";
  public void on() {
  if(Storage.user(s) == true) {
  //That part will be my question
   }
 }
}

So, if the value of the given String is true, there should be an extra HashMap, like in the Storage class. But I mean, there should be to every String (if it´s true) a HashMap and they shouldnt share one Map. As I said, I dont know that much about maps and maybe HashMaps are completely false. If so, I´m sorry. Hopefully you get my problem and know a suitable solution

Upvotes: 0

Views: 172

Answers (2)

Andreas
Andreas

Reputation: 159215

If you want a separate Map<Integer, Double> for each user, as identified by a String, then create a Map<String, Map<Integer, Double>>.

You don't need a map to Boolean, because you can check for the existence of a user using containsKey(), or simply get the value of the outer map and check for null.

class Storage {
    public static Map<String, Map<Integer, Double>> user = new HashMap<>();
}
Map<Integer, Double> money = Storage.map.get(userName);
if (money != null) {
    // code here
}

UPDATE

When you are building the map, use the computeIfAbsent() and merge() methods, like this:

class Storage {
    public static Map<String, Map<Integer, Double>> user = new HashMap<>();

    public static void addUserMoney(String userName, int moneyKey, double amount) {
        user.computeIfAbsent(userName, k -> new HashMap<>())
            .merge(moneyKey, amount, Double::sum);
    }
}

Test

Storage.addUserMoney("John", 1, 9.95);
Storage.addUserMoney("Jane", 3, 3.75);
Storage.addUserMoney("John", 1, 4.25);
System.out.println(Storage.user);

Output

{John={1=14.2}, Jane={3=3.75}}

Upvotes: 1

WJS
WJS

Reputation: 40057

What you need is a map of maps.

Map<String, Map<Integer,Double>> map = new HashMap<>();

Then you can just put the value there for the String that is true.

Examples:

Map<String, Map<Integer,Double>> map = new HashMap<>();

The following:

Map.computeIfAbsent(Key, (a)->new HashMap<>()).put(i, d);

Behaves the same way as

Map<Integer,Double> innerMap = map.get(Key);
if (innerMap == null) {
     innerMap = new HashMap<>();
     map.put(Key, innerMap)
}
innerMap.put(i, d);

Assign some values


map.computeIfAbsent("A", (a)->new HashMap<>()).put(10,20.56);
map.computeIfAbsent("A", (a)->new HashMap<>()).put(20,4.2);
map.computeIfAbsent("A", (a)->new HashMap<>()).put(30,10.6);

map.computeIfAbsent("B", (a)->new HashMap<>()).put(1,2.);
map.computeIfAbsent("B", (a)->new HashMap<>()).put(2,2.44);
map.computeIfAbsent("B", (a)->new HashMap<>()).put(10,2.45);

map.computeIfAbsent("C", (a)->new HashMap<>()).put(30,3.2);

Print all the values

map.entrySet().forEach(System.out::println);

To get a specific Value

double d = map.get("A").get(10); // gets 20.56 from Map "A"
System.out.println(d); 

If you already know the inner map is associated with a key you can just do the following:

// gets the map at "C"
// and puts 44.7 at key 44
map.get("C").put(44,44.7);

Upvotes: 0

Related Questions