集合之集合使用(二)

一.  Properties   (最重要,经常使用,读取配置文件)  为HashTabel的子类
      Properties prop = new Properties();
     
           prop.load(new FileInputStream(new File("AAA")));        //这里是2种加载读取文件方式
           p.load(PropertiesUtil.class.getResourceAsStream("menu.txt"));  (注:这里都要使用Try/Catch语句)

           p.put("白菜", "35");                           //这个方法只是将数据写入内存

           prop.store(new FileOutputStream(new File("AAA")), null);  //将内容写入文件

           String psw = prop.getProperty(user);               //根据键查询对应的值
 
  
              Set<Object> set = prop.keySet();                    //迭代法查键
       Iterator it = set.iterator();
       while(it.hasNext()){
   Object o = it.next();
   System.out.println(o);      
       }


      Collection col = prop.values();                    //迭代法查值
      Iterator itt = col.iterator();
      while(itt.hasNext()){
  Object o = itt.next();
  System.out.println(o);
      }


二.LinkedList                          (注:在不考虑线性安全时,建议都使用ArrayList,效率更高点)
           //迭代器查询链表LinkedList
               LinkedList<String> list = new LinkedList<String>();
           list.add("asd");
    list.add("ddd");
    list.add("qqq");
    System.out.println(list.size());
    System.out.println(list.get(2));
    Iterator<String> it = list.iterator();
    while(it.hasNext()){
    String str = it.next();
    System.out.println(str);
  }


三.HashMap
      
                HashMap<String,String> hash = new HashMap<String,String>();
  hash.put("asd", "qwe");
  hash.put("asb", "sss");

                还可以用Get方法获得值

  Set<String> set = hash.keySet();
  Iterator<String> itt = set.iterator();
  while(itt.hasNext()){
   String ss = itt.next();
   System.out.println(ss);
  }
  
  Collection col = hash.values();
  Iterator it = col.iterator();
  while(it.hasNext()){
   Object o = it.next();
   System.out.println(o);
  }

               //直接查值
  Collection<String> coll= hash.values();
  System.out.println(coll);
 

       总结: 其他常用集合可以直接查询JDK使用,都比较简单,重点是记住他们对应的接口,就可以知道使用方法。
              如下:
        
              ArrayList/Vector  >>List
              HashSet/TreeSet   >>Set      Collection(接口)    add()方法

              Propeties/HashTable
              TreeMap/HashMap              Map(接口)           put()方法

 

 

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值