-1

I have two HashMap objects defined as:

Map<String, String> requestParams = new HashMap<>();
Map<String, Boolean> requestParamForOauth = new HashMap<>();

How can I merge these two maps?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    `Map all = new HashMap<>();` and some `putAll`? –  Apr 12 '17 at 06:10
  • What??? World is getting weirder.. It would be better if you ask what is your problem that you are trying to solve using these HashMap. What do you want to achieve? This is not a good way to do, whatever you are doing? – Arjun Chaudhary Apr 12 '17 at 06:18
  • Are the keys of the two maps the same? It's not clear what you want to do: (1) the keys of the maps are the same, and you want to create a map that maps the key to _both_ the `String` and `Boolean` values (by creating a new object that holds both values); (2) the keys of the maps are different, and you want a map that can map a key to _either_ a `String` or a `Boolean`. GhostCat and I are guessing #1, almost everyone else is guessing #2. Which is it? – ajb Apr 12 '17 at 06:39

4 Answers4

1

Assuming that both maps contain the same set of keys, and that you want to "combine" the values, the thing you would be looking for is a Pair class, see here for example.

You simply iterate one of the maps; and retrieve values from both maps; and create a Pair; and push that in your result map.

The only downside is that there is no "official" Pair class that you could use (see here for more thoughts around that).

Alternatively, if there is a "deeper" meaning of those "combined" values (beyond a simple "tuple/pair" semantics), you could instead create your own class that wraps around those two values.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • If the pair of (String, Boolean) represents something, it might be worthwhile for OP to create his own class with a descriptive name, instead of using Pair. Also, we don't know whether there could be keys that exist in one map but not the other, and whether that's possible both ways; if so, some additional logic will be needed. – ajb Apr 12 '17 at 06:35
0

Your keys are of the same type (String), but the values are not even related by an interface or super class, you will need to define a Map<String, Object> and make use of the Map#putAll method

Map<String, String> requestParams = new HashMap<>();
Map<String, Boolean> requestParamForOauth = new HashMap<>();
Map<String, Object> requestParamForOauth2 = new HashMap<>();
requestParamForOauth2.putAll(requestParams);
requestParamForOauth2.putAll(requestParamForOauth);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

If you want to use one list to store all the data You can use one HashMap<String,Object>

Shira Elitzur
  • 433
  • 3
  • 9
0

What do you want to do when the same key appears in both Map? If you want to keep both the String and Boolean, then you'll need a map that looks like this: Map<String, Pair(String, Boolean)>. If you just want to keep one value, then Map<String, Object> is what you want.

Dat Nguyen
  • 1,626
  • 22
  • 25