Map

public class MapTest {

@SuppressWarnings("rawtypes")
public static void main(String[] args) {
//HashMap
System.out.println("------HashMap无序输出------");
HashMap<String,String> hsMap=new HashMap<String,String>();
hsMap.put("3", "Value3");
hsMap.put("1", "Value1");
hsMap.put("2", "Value2");
hsMap.put("b", "ValueB");
hsMap.put("a", "ValueA");
Iterator it = hsMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());
}

//TreeMap
System.out.println("------TreeMap按Key排序输出------");
TreeMap<String,String> teMap=new TreeMap<String,String>();
teMap.put("3", "Value3");
teMap.put("1", "Value1");
teMap.put("2", "Value2");
teMap.put("b", "ValueB");
teMap.put("a", "ValueA");
Iterator tit = teMap.entrySet().iterator();
while (tit.hasNext()) {
Map.Entry e = (Map.Entry) tit.next();
System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue());
}
}

}

posted @ 2016-11-03 14:11  造西  阅读(88)  评论(0编辑  收藏  举报