英文:
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(", "));
String values = map.values().stream().map(obj -> String.valueOf(obj)).collect(Collectors.joining(", "));
, main
function
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
答案2
得分: 1
HashMap<String, Object> yourHashMap = new Gson().fromJson(dynamicJson, HashMap.class);
LinkedHashSet
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<String, Object> yourHashMap = new Gson().fromJson(dynamicJson, HashMap.class);
LinnkedHashSet<String> keys = new LinnkedHashSet<>();
LinnkedHashSet<String> values = new LinnkedHashSet<>();
yourHashMap.forEach((k, v) -> {
keys.add(k);
values.add(v);
});
System.out.println("keys: "+String.join(",",keys) +
"\n values: "+ String.join(",",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<String, String> yourHashMap = ....
String keys = String.join(",", yourHashMap.keySet());
String values = String.join(",", yourHashMap.values());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论