basic java-4

1.泛型-->只在編譯階段有作用,在運行階段會丟棄具體的類型

ArrayList<Integer> collection1 = new ArrayList<Integer>();
ArrayList<String> collection2 = new ArrayList<String>();
System.out.println(collection1.getClass() == collection2.getClass())

結果:true

 

2.泛型小例子

HashMap<String, Integer> maps = new HashMap<String, Integer>();
		maps.put("阿安", 18);
		maps.put("阿三", 40);
		maps.put("狗四", 33);
		Set<Map.Entry<String,Integer>> entrySet = maps.entrySet(); //爲了能使hashmap疊代,使用enreySet轉化爲Set
		for(Map.Entry<String, Integer> entry : entrySet){
			System.out.println(entry.getKey() + " : " + entry.getValue());
} 
發佈了27 篇原創文章 · 獲贊 0 · 訪問量 452
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章