将HashMap的键和值分别存储到两个字符串变量中,在Java中。

huangapple go评论101阅读模式
英文:

Store HashMap keys and values to two separate string variables in Java

问题

I need to store all keys into single string variable each key separated by a comma and also I need to do the same for all values

Here is my code

  1. HashMap<String, Object> yourHashMap = new Gson().fromJson(dynamicJson, HashMap.class);
  2. yourHashMap.forEach((k, v) -> {
  3. //System.out.println("Key: " + k );
  4. String result = k + ",";
  5. System.out.println("Keys : "+result);
  6. });

Actual output Keys : name, message,

Expected output : Keys : name, message

Values : "Message1", "Message Content"

Using these outputs I'm going to create CSV file it uses keys as header and values as rows

英文:

I need to store all keys into single string variable each key separated by a comma and also I need to do the same for all values

Here is my code

  1. HashMap<String, Object> yourHashMap = new Gson().fromJson(dynamicJson, HashMap.class);
  2. yourHashMap.forEach((k, v) -> {
  3. //System.out.println("Key: " + k );
  4. String result = k + ",";
  5. System.out.println("Keys : "+result);
  6. });

Actual output Keys : name,
message,

> Expected output : Keys : name, message
>
> Values : "Message1", "Message Content"

Using these outputs I'm going to create CSV file it uses keys as header and values as rows

答案1

得分: 3

你可以使用Collectors.joining()来添加逗号分隔:

  1. String keys = map.keySet().stream().collect(Collectors.joining(", "));
  2. String values = map.values().stream().map(obj -> String.valueOf(obj)).collect(Collectors.joining(", "));

main 函数

  1. public static void main(String[] args) {
  2. Map<String, String> map = new HashMap<>();
  3. map.put("key1", "val1");
  4. map.put("key2", "val2");
  5. map.put("key3", "val3");
  6. map.put("key4", "val4");
  7. String keys = map.keySet().stream().collect(Collectors.joining(", "));
  8. String values = map.values().stream().map(obj -> String.valueOf(obj)).collect(Collectors.joining(", "));
  9. System.out.println("Keys: " + keys);
  10. System.out.println("Values: " + values);
  11. }

output

  1. Keys: key1, key2, key3, key4
  2. Values: val1, val2, val3, val4
英文:

You can use Collectors.joining() to add comma separation

  1. String keys = map.keySet().stream().collect(Collectors.joining(&quot;, &quot;));
  2. String values = map.values().stream().map(obj -&gt; String.valueOf(obj)).collect(Collectors.joining(&quot;, &quot;));

, main function

  1. public static void main(String[] args) {
  2. Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  3. map.put(&quot;key1&quot;, &quot;val1&quot;);
  4. map.put(&quot;key2&quot;, &quot;val2&quot;);
  5. map.put(&quot;key3&quot;, &quot;val3&quot;);
  6. map.put(&quot;key4&quot;, &quot;val4&quot;);
  7. String keys = map.keySet().stream().collect(Collectors.joining(&quot;, &quot;));
  8. String values = map.values().stream().map(obj -&gt; String.valueOf(obj)).collect(Collectors.joining(&quot;, &quot;));
  9. System.out.println(&quot;Keys: &quot; + keys);
  10. System.out.println(&quot;Values: &quot; + values);
  11. }

, output

  1. Keys: key1, key2, key3, key4
  2. Values: val1, val2, val3, val4

答案2

得分: 1

HashMap<String, Object> yourHashMap = new Gson().fromJson(dynamicJson, HashMap.class);
LinkedHashSet keys = new LinkedHashSet<>();
LinkedHashSet values = new LinkedHashSet<>();
yourHashMap.forEach((k, v) -> {
keys.add(k);
values.add(v.toString());
});
System.out.println("keys: " + String.join(",", keys) +
"\n values: " + String.join(",", values));

英文:
  1. HashMap&lt;String, Object&gt; yourHashMap = new Gson().fromJson(dynamicJson, HashMap.class);
  2. LinnkedHashSet&lt;String&gt; keys = new LinnkedHashSet&lt;&gt;();
  3. LinnkedHashSet&lt;String&gt; values = new LinnkedHashSet&lt;&gt;();
  4. yourHashMap.forEach((k, v) -&gt; {
  5. keys.add(k);
  6. values.add(v);
  7. });
  8. System.out.println(&quot;keys: &quot;+String.join(&quot;,&quot;,keys) +
  9. &quot;\n values: &quot;+ String.join(&quot;,&quot;,values));

答案3

得分: 1

使用 String.join

  1. HashMap<String, String> yourHashMap = ....
  2. String keys = String.join(",", yourHashMap.keySet());
  3. String values = String.join(",", yourHashMap.values());
英文:

Use String.join

  1. HashMap&lt;String, String&gt; yourHashMap = ....
  2. String keys = String.join(&quot;,&quot;, yourHashMap.keySet());
  3. String values = String.join(&quot;,&quot;, yourHashMap.values());

huangapple
  • 本文由 发表于 2020年7月30日 04:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63162000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定