集合之五

HashMap
HashMap对key进行散列。
keySet()、values()、entrySet()。
TreeMap
TreeMap按照key进行排序。
HashMap和TreeMap的比较

import  java.util. * ;
class  HashMapTest
{
    
public static void printElements(Collection c)
    
{
        Iterator it
=c.iterator();
        
while(it.hasNext())
        
{
            System.out.println(it.next());
        }

    }

    
public static void main(String[] args)
    
{
        HashMap hm
=new HashMap();
        hm.put(
"one","zhangsan");
        hm.put(
"two","lisi");
        hm.put(
"three","wangwu");
        
        System.out.println(hm.get(
"one"));
        System.out.println(hm.get(
"two"));
        System.out.println(hm.get(
"three"));
        
        
        Set keys
=hm.keySet();
        System.out.println(
"Key:");
        printElements(keys);
        
        Collection values
=hm.values();
        System.out.println(
"Value:");
        printElements(values);
        
        Set entry
=hm.entrySet();
        
//printElements(entry);
        Iterator it=entry.iterator();
        
while(it.hasNext())
        
{
            Map.Entry me
=(Map.Entry)it.next();
            System.out.println(me.getKey()
+":"+me.getValue());
        }

        
    }

}

和Set类似,HashMap的速度通常都比TreeMap快,只有在需要排序的功能的时候,才使用TreeMap。
Java1.0/1.1的集合类
Vector:用ArrayList代替Vector。
Hashtable:用HashMap代替Hashtable。
Satck:用LinkedList代替Stack。
Properties

import  java.util. * ;
import  java.io. * ;
class  PropTest
{
    
public static void main(String[] args)
    
{
        
/*Properties pps=System.getProperties();
        pps.list(System.out);
*/

        Properties pps
=new Properties();
        
try
        
{
            pps.load(
new FileInputStream("winsun.ini"));
            Enumeration 
enum=pps.propertyNames();
            
while(enum.hasMoreElements())
            
{
                String strKey
=(String)enum.nextElement();
                String strValue
=pps.getProperty(strKey);
                
                System.out.println(strKey
+"="+strValue);
            }

        }

        
catch(Exception e)
        
{
            e.printStackTrace();
        }

    }

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值