1.获取Entity集合,使用foreach遍历
2.获取key集合,value集合,分别遍历
3.获取Entity迭代器进行遍历
4.获取key集合,通过get方法获取value
推荐使用第一种,效率高,key和value都有,第四种不推荐,效率较低。
SystemTest.java
1 import java.util.Iterator; 2 import java.util.Map; 3 import java.util.Properties; 4 import java.util.Set; 5 6 /** 7 * Created by gxf on 2016/12/8. 8 */ 9 public class SystemTest { 10 public static void main(String args[]){ 11 getEnv(); 12 } 13 14 public static void getProperties(){ 15 Properties properties = System.getProperties(); 16 Iterator iterator = properties.entrySet().iterator(); 17 while(iterator.hasNext()){ 18 Map.Entry e = (Map.Entry)iterator.next(); 19 System.out.println(e.getKey() + "," + e.getValue()); 20 } 21 } 22 23 public static void getEnv(){ 24 Map<String, String> mapOfEnv = System.getenv(); 25 //first 26 for(Map.Entry<String, String> entry : mapOfEnv.entrySet()){ 27 System.out.println(entry.getKey() + ", " + entry.getValue()); 28 } 29 30 //second 31 for(String key : mapOfEnv.keySet()){ 32 System.out.println("key = " + key); 33 } 34 for(String value : mapOfEnv.values()){ 35 System.out.println("value = " + value); 36 } 37 38 //third 39 Iterator<Map.Entry<String, String>> iterator = mapOfEnv.entrySet().iterator(); 40 while(iterator.hasNext()){ 41 Map.Entry<String, String> entry = iterator.next(); 42 System.out.println(entry.getKey() + ", " + entry.getValue()); 43 } 44 45 //fourth 46 Set<String> setOfKey = mapOfEnv.keySet(); 47 for(String key : setOfKey){ 48 System.out.println(key + ", " + mapOfEnv.get(key)); 49 } 50 } 51 }