码迷,mamicode.com
首页 > 其他好文 > 详细

集合泛型

时间:2018-03-09 01:42:20      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:哈希算法   col   遍历   方式   shm   lis   print   arraylist   ring   

Collection  接口  用父类引用指向子类对象

  List(存取有序,有索引,可以重复)

    ArrayList底层是数组实现的,线程不安全,查找和修改快,增和删比较慢

    LinkedList底层是链表实现的,线程不安全,增和删比较快,查找和修改比较慢

    Vector底层是数组实现的,线程安全的,无论增删改查都慢

      如果查找和修改多,用ArrayList

      如果增和删多,用LinkedList

      如果都多,用ArrayList

  Set(存取无序,无索引,不可以重复)

    HashSet 底层是哈希算法实现。线程不安全,存取速度快。

      LinkedHashSet 底层是链表实现,但是也是可以保证元素唯一,和HashSet原理一样

    TreeSet 底层是二叉树算法实现 一般在开发的时候不需要对存储的元素排序,所以在开发的时候大多用HashSet,HashSet的效率比较高

      TreeSet在面试的时候比较多,问你有几种排序方式,和几种排序方式的区别

  Map

    HashMap底层是哈希算法,针对键线程不安全,存取速度快。可以最多一个为空

      LinkedHashMap底层是链表,针对键

    TreeMap底层是二叉树算法,针对键。默认根据键排序

    Hashtable它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢

附:map 遍历的四种方法:
public static void main(String[] args) {
  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "value1");
  map.put("2", "value2");
  map.put("3", "value3");
  //第一种:普遍使用,二次取值
  System.out.println("通过Map.keySet遍历key和value:");
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }
  
  //第二种
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  //第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  //第四种
  System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }
}

 

集合泛型

标签:哈希算法   col   遍历   方式   shm   lis   print   arraylist   ring   

原文地址:https://www.cnblogs.com/test1234/p/8531496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!