zoukankan      html  css  js  c++  java
  • 【Java】 遍历HashMap

    1.遍历键值对

      使用map.entrySet(),注意foreach语句中的类型为Map.Entry<K, V>

    2.遍历Key

      使用map.keySet()

    3.遍历Value

      使用map.values()

    	public static void main(String[] args) {
    		HashMap<String, Integer> map = new HashMap<String, Integer>();
    		map.put("一", 1);
    		map.put("二", 2);
    		map.put("三", 3);
    		
    		// 1.遍历键值对,使用Map.Entry,map.entrySet()
    		System.out.println("=====遍历键值对=====");
    		for (Map.Entry<String, Integer> i : map.entrySet()) {
    
    			System.out.print("Key:" + i.getKey() + "	");
    			System.out.println("Value:" + i.getValue());
    		}
    
    		// 2.遍历Key,使用map.keySet()
    		System.out.println("=====遍历Key=====");
    		for (String i : map.keySet()) {
    			System.out.println("Key:" + i);
    		}
    		
    		// 3.遍历Value,使用map.entrySet
    		System.out.println("=====遍历Value=====");
    		for (int i : map.values()) {
    			System.out.println("Value:" + i);
    		}		
    	}
    

      

    =====遍历键值对=====
    Key:一    Value:1
    Key:三    Value:3
    Key:二    Value:2
    =====遍历Key=====
    Key:一
    Key:三
    Key:二
    =====遍历Value=====
    Value:1
    Value:3
    Value:2
  • 相关阅读:
    vbScript首字拼音查询
    C#读取U盘序列号
    下拉 回车 筛选
    Oracle的汉字转拼音首字母的函数
    sql 触发器禁止和启用
    List.FindAll 方法
    MyControl 常用操作
    List.Find 方法
    regsvr32 C:\WINDOWS\system32\cell32.ocx
    2222222
  • 原文地址:https://www.cnblogs.com/yongh/p/10009440.html
Copyright © 2011-2022 走看看