There are various ways to achieve this. Here are three.
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println("using entrySet and toString");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
System.out.println();
System.out.println("using entrySet and manual string creation");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println();
System.out.println("using keySet");
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
System.out.println();
Output
using entrySet and toString
key1=value1
key2=value2
key3=value3
using entrySet and manual string creation
key1=value1
key2=value2
key3=value3
using keySet
key1=value1
key2=value2
key3=value3
Answer from Stefan Dollase on Stack OverflowThere are various ways to achieve this. Here are three.
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println("using entrySet and toString");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
System.out.println();
System.out.println("using entrySet and manual string creation");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println();
System.out.println("using keySet");
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
System.out.println();
Output
using entrySet and toString
key1=value1
key2=value2
key3=value3
using entrySet and manual string creation
key1=value1
key2=value2
key3=value3
using keySet
key1=value1
key2=value2
key3=value3
Inside of your loop, you have the key, which you can use to retrieve the value from the Map:
for (String key: mss1.keySet()) {
System.out.println(key + ": " + mss1.get(key));
}
Using a map like that obfuscates the parameters, making it so developers have to read the method to know what parameters to use. Worse, these kinds of maps are case sensitive. I would strongly recommend you don't do this. If you have enough parameters that you'd want to do this, make a wrapper or Data Transfer Object.
Just some general notes:
- sfdcfox's answer is, as always, brilliant. Using a map like that isn't a great idea, and I'd go so far as to say it's a bad idea.
- I like your desire to let other developers know what the parameters are, but I think the right way to solve this is with proper documentation. Apex-recipes (https://github.com/trailheadapps/apex-recipes) uses the npm package "@cparra/apexdocs" to generate standardized markdown files for classes. The per-method documentation block looks like this:
/**
- @description The doLoad method is required by the CacheBuilder interface
- This method needs to return an Object - and that's the key to the Cache
- builder interface - You must have this method return a single Object that
- is either calculated by this method, or returned from the Cache by the
- key
- @param key String used to help generate the Cache Key
- @return
ObjectThis object should be casted at the call location - @group Platform Cache Recipes
- @see CanTheUser
- @example Account[] accounts = (Account[]) Cache.Session.get(PlatformCacheBuilderRecipes.class, 'myAccounts') */ public Object doLoad(String key) {
So that's not rendering great, but those bullet points are *'s
Apexdocs understands certain tags, like @description and when run it generates docs like this markdown.
---
layout: default
---
# PlatformCacheBuilderRecipes class
demonstrates how to use the Cache.CacheBuilder Interface
---
## Methods
### `doLoad(String key)` → `Object`
The doLoad method is required by the CacheBuilder interface This method needs to return an Object - and that's the key to the Cache builder interface - You must have this method return a single Object that is either calculated by this method, or returned from the Cache by the key
#### Parameters
|Param|Description|
|-----|-----------|
|`key` | String used to help generate the Cache Key |
#### Return
**Type**
Object
**Description**
`Object` This object should be casted at the call location
#### Example
```java
Account[] accounts = (Account[]) Cache.Session.get(PlatformCacheBuilderRecipes.class, 'myAccounts')
```
---
Generating and making this kind of documentation available makes it easy for your third party developers to consume your methods, and readily identify what order the parameters are in.
Use Object#toString().
String string = map.toString();
That's after all also what System.out.println(object) does under the hoods. The format for maps is described in AbstractMap#toString().
Returns a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map's
entrySetview's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as byString.valueOf(Object).
You can also use google-collections (guava) Joiner class if you want to customize the print format
Joiner.on(",").withKeyValueSeparator("=").join(map);