java集合类(六)HashMap与TreeMap的应用

来源:互联网 发布:淘宝美女图片大全2015 编辑:程序博客网 时间:2024/05/16 12:15

HashMap与TreeMap应用(二者应用基本相同,TreeMap多用于排序中,
                      下面仅以HashMap为应用实例进行介绍)
  import java.util.*;
public class HashMapTest {
    public static void printElements(Collection c)
    {
        Iterator i=c.iterator();
        while(i.hasNext())
            System.out.println(i.next());
    }
    public static void main(String[] args)
    {
        HashMap h=new HashMap();
        h.put("one", "qy");
        h.put("two", "lk");
        h.put("three","lkp");
        h.put("four","zhm");
        System.out.println(h.get("one"));
        System.out.println(h.get("two"));
       
        Collection keys=h.keySet();
        Collection values=h.values();
        System.out.println("key:");
        HashMapTest.printElements(keys);
        System.out.println("value:");
        printElements(values);  //可以直接引用,同上面相区分
        Set entry=h.entrySet(); //打印输出为键值对Map.Entry类:two=l,one=qy, three=lkp, four=zhm;
        printElements(entry);
       
    }

}

原创粉丝点击