Map遍历示例

package collections;
 
import java.util.HashMap;
 
public class HashMapDemo {
 
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<String, String>();
        // 添加内容,内容以key/value方式填入
        map.put("湘南IT培训", "https://www.0735it.com/");
        map.put("google","https://www.google.com/");
        map.put("利永贞网","https://www.liyongzhen.com");
         
        // java 5 for 方式遍历;后进先出来
        for(String key:map.keySet()) {
            System.out.printf("key %s;\nvalue %s\n",key,map.get(key));
        }
         
        // java 8 forEach 方式遍历
        map.entrySet().forEach(e->               
                System.out.printf("key %s;\nvalue %s\n",e.getKey(),e.getValue())        
        );
    }
 
}

运行结果

key 利永贞网;
value https://www.liyongzhen.com
key google;
value https://www.google.com/
key 湘南IT培训;
value https://www.0735it.com/