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

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

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

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

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

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

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()来添加逗号分隔:

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

main 函数

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

output

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

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

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

, main function

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

, output

Keys: key1, key2, key3, key4
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));

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

答案3

得分: 1

使用 String.join

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

Use String.join

HashMap&lt;String, String&gt; yourHashMap = ....
String keys = String.join(&quot;,&quot;, yourHashMap.keySet());
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:

确定