我是Java新手,正在尝试学习地图的概念。
我想出了下面的代码。但是,我想同时打印出“key String”和“value String”。
ProcessBuilder pb1 = new ProcessBuilder();
Map<String, String> mss1 = pb1.environment();
System.out.println(mss1.size());
for (String key: mss1.keySet()){
System.out.println(key);
}
我只能找到只打印“键字符串”的方法。
答案 0 :(得分:17)
有多种方法可以实现这一目标。这是三个。
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
答案 1 :(得分:4)
在循环内部,您有密钥,您可以使用该密钥从Map
中检索值:
for (String key: mss1.keySet()) {
System.out.println(key + ": " + mss1.get(key));
}
答案 2 :(得分:2)
col1 <- c('1/2 in. x 1/2 in. x 3/4 ft. Copper Pressure Cup', 'Ensemble 60 in. x 43-1/2 in. x 54-1/4 in. 3-piece',
'2-3/4 in. x 4-1/2 in. Heavy-Duty',
'1/4-20 x 2 in. Forged Steel ',
'1/2-Amp Slo-Blo GMA Fuse',
'3/4 in. x 12 in. x 24 in. White Thermally',
'12.0 oz. of weight',
'1.4 fl. oz. of liquid',
'14 gal. tall',
'Sahara Wood 47 in. Long x 12-1/8 in. Deep x 1-11/16 in. Height',
'1/25 HP Cast Iron ',
'1/2 in., 3/4 in. and 1 in. PVC ',
'24-3/4 in. x 48-3/4 in. x 1-1/4 in. Faux Windsor',
'8 oz. -200 Pot Of Cream',
'5/8 in. dia. x 25 ft. Water ',
'18.5 / 30.5 in. Brushed Nickel',
'57-1/2 in. x 70-5/16 in. Semi-Framed',
'2-1/4 HP Router',
'12-Volt Lithium-Ion Cordless 3/8 in.',
'12-Gauge 24-5/8 in. Strap ',
'7-3/4 in. Wigan Ceiling',
'1 qt. B-I-N ',
'3/8 in. O.D. x 1/4 in. NPTF',
'2-1/2 in. Long x 5/8 in. Diameter Spring',
'1/4 x 3 in. Heat-Shrink ',
'4-White PVC End',
'41000 Series Non-Vented Range',
'Revival 1-Spray 5-Katalyst Air',
'180-Degree White Outdoor',
'3/8 x 3 Hand Scraped ',
'67-Qt. Jug',
'35-77-7/8 in. White',
'-16 tpi x 4 in. Stainless Steel',
'3-21 degree Full')
df <- data.frame(col1 = col1)
df
col1
1 1/2 in. x 1/2 in. x 3/4 ft. Copper Pressure Cup
2 Ensemble 60 in. x 43-1/2 in. x 54-1/4 in. 3-piece
3 2-3/4 in. x 4-1/2 in. Heavy-Duty
4 1/4-20 x 2 in. Forged Steel
5 1/2-Amp Slo-Blo GMA Fuse
6 3/4 in. x 12 in. x 24 in. White Thermally
7 12.0 oz. of weight
8 1.4 fl. oz. of liquid
9 14 gal. tall
10 Sahara Wood 47 in. Long x 12-1/8 in. Deep x 1-11/16 in. Height
11 1/25 HP Cast Iron
12 1/2 in., 3/4 in. and 1 in. PVC
13 24-3/4 in. x 48-3/4 in. x 1-1/4 in. Faux Windsor
14 8 oz. -200 Pot Of Cream
15 5/8 in. dia. x 25 ft. Water
16 18.5 / 30.5 in. Brushed Nickel
17 57-1/2 in. x 70-5/16 in. Semi-Framed
18 2-1/4 HP Router
19 12-Volt Lithium-Ion Cordless 3/8 in.
20 12-Gauge 24-5/8 in. Strap
21 7-3/4 in. Wigan Ceiling
22 1 qt. B-I-N
23 3/8 in. O.D. x 1/4 in. NPTF
24 2-1/2 in. Long x 5/8 in. Diameter Spring
25 1/4 x 3 in. Heat-Shrink
26 4-White PVC End
27 41000 Series Non-Vented Range
28 Revival 1-Spray 5-Katalyst Air
29 180-Degree White Outdoor
30 3/8 x 3 Hand Scraped
31 67-Qt. Jug
32 35-77-7/8 in. White
33 -16 tpi x 4 in. Stainless Steel
34 3-21 degree Full