Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 地图<;字符串,字符串>;,如何同时打印;键串;及;值字符串";在一起_Java_Dictionary_Key - Fatal编程技术网

Java 地图<;字符串,字符串>;,如何同时打印;键串;及;值字符串";在一起

Java 地图<;字符串,字符串>;,如何同时打印;键串;及;值字符串";在一起,java,dictionary,key,Java,Dictionary,Key,我是Java新手,正在尝试学习地图的概念 我已经想出了下面的代码。但是,我想同时打印“键字符串”和“值字符串” ProcessBuilder pb1 = new ProcessBuilder(); Map<String, String> mss1 = pb1.environment(); System.out.println(mss1.size()); for (String key: mss1.keySet()){ System.out.println(key); }

我是Java新手,正在尝试学习地图的概念

我已经想出了下面的代码。但是,我想同时打印“键字符串”和“值字符串”

ProcessBuilder pb1 = new ProcessBuilder();
Map<String, String> mss1 = pb1.environment();
System.out.println(mss1.size());

for (String key: mss1.keySet()){
    System.out.println(key);
}
ProcessBuilder pb1=新的ProcessBuilder();
Map mss1=pb1.environment();
System.out.println(mss1.size());
用于(字符串键:mss1.keySet()){
系统输出打印项次(键);
}

我只能找到只打印“键字符串”的方法。

在循环中,您有键,可以使用它从
映射中检索值:

for (String key: mss1.keySet()) {
    System.out.println(key + ": " + mss1.get(key));
}
final Map mss1=new ProcessBuilder().environment();
mss1.entrySet()
.stream()
//根据您想要加入K和V的方式,请使用不同的分隔符
.map(条目->
String.join(“:”,entry.getKey(),entry.getValue())
.forEach(System.out::println);

实现这一点有多种方法。这里有三个

    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();

System.out.println(key+”,“+mss1.get(key))
apache.commons.lang.StringUtils的一行代码:
String message=StringUtils.join(yourMap.entrySet().toArray(),“\n”)使用Map.Entry而不是Entry
    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();
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