遍历循环输出map的几种方式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                遍历循环输出map的几种方式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                package 常用的遍歷方法.forTest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class CircleTest {
	public static void main(String[] args) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("accident", "意外險(xiǎn)");
		map.put("properties", "財(cái)產(chǎn)險(xiǎn)");
		map.put("biz", "商業(yè)險(xiǎn)");
		map.put("force", "交強(qiáng)險(xiǎn)");
		
		//1、迭代器
		Iterator<Map.Entry<String, Object>> iter = map.entrySet().iterator();
		//判斷往下還有沒有數(shù)據(jù)
		while(iter.hasNext()){
			//有的話取出下面的數(shù)據(jù)
			Entry<String, Object> entry = iter.next();
			String key = entry.getKey();
			String value = (String)entry.getValue();
			if("biz".equals(entry.getKey())){
				System.out.println("這個(gè)是商業(yè)險(xiǎn)");
			}
			System.out.println(key + " :" + value);
		}
		
		//2、foreach循環(huán)
		//獲取key + value
		for (Object key : map.keySet()) {
			String value = (String)map.get(key);
			System.out.println(key + " : " + value);
		}
		//獲取value
		for (Object value : map.values()) {
			System.out.println(value);
		}
		
		//3、當(dāng)容量特別大的時(shí)候
		for (Entry<String, Object> entry : map.entrySet()) {
			/*iter :  biz=商業(yè)險(xiǎn)
					force=交強(qiáng)險(xiǎn)
					accident=意外險(xiǎn)
					properties=財(cái)產(chǎn)險(xiǎn)*/
			 System.out.println(entry.getKey() + " : " + entry.getValue());
		}
	}
}
總結(jié)
以上是生活随笔為你收集整理的遍历循环输出map的几种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: android tab 点击,TabLa
 - 下一篇: ionic android 版本号,io