java_集合框架

一、集合框架图

二、Collection接口

    Collection中可以存储的元素间无序可以重复的元素。
    Collection接口的子接口List和Set,Map不是Collection的子接口。

三、List接口

List接口中的元素的特点:

    List中的元素有序,可以重复。

    两个常用的实现类ArrayList和LinkedList
    1,ArrayList
        类似数组形式存储,访问数度快,增删慢,线程不安全。
        Vector是ArrayList的多线程的一个替代品。
        ArrayList遍历方式:
public static void main(String[] args) {
     List<String> list=new ArrayList<String>();
     list.add("111");
     list.add("222");
     list.add("333");
     //第一种遍历方法使用foreach遍历List
     for (String str : list) {//也可以改写for(int i=0;i<list.size();i++)这种形式
        System.out.println(str);
     }
 
     //第二种遍历,把链表变为数组相关的内容进行遍历
     String[] strArray=new String[list.size()];
     list.toArray(strArray);
     for(int i=0;i<strArray.length;i++) //这里也可以改写为foreach(String str:strArray)这种形式
     {
         System.out.println(strArray[i]);
     }
     
     //第三种遍历 使用迭代器进行相关遍历
     
     Iterator<String> ite=list.iterator();
     while(ite.hasNext())
     {
         System.out.println(ite.next());
     }
 }
    2,LinkedList
        类似链表结果,查询慢,增删快,线程不安全。
        LinkedList遍历方式:
public static void main(String[] args) {
     
    List<String> list=new LinkedList<String>();
    list.add("111");
    list.add("222");
    list.add("333");
    //LinkedList遍历的第一种方式使用数组的方式
    String[] strArray=new String[list.size()];
    list.toArray(strArray);
    for(String str:strArray)
    {
        System.out.println(str);
    }
    //LinkedList遍历的第二种方式
    for(String str:list)
    {
        System.out.println(str);   
    }
}

四、Set接口

    Set中的元素无序,不重复。
    虽然Set中元素没有顺序,但是元素在set中的位置是有由该元素的HashCode决定的,其具体位置其实是固定的。
    Set集合中去重和Hashcode与equals方法之间相关。
    常见实现类有HashSet,LinedHashSet和TreeSet
    1,HashSet
        底层基于Hash算法进行存储元素,允许null,无序,不重复,元素位置固定
        HashSet是通过HashMap实现的。
        HashSet的几种遍历方法:
public static void main(String[] args) {
     Set<String> set=new HashSet<String>();
     set.add("111");
     set.add("222");
     set.add("333");
     //遍历集合的第一种方法,使用数组的方法
     String[] strArray=new String[set.size()];
     strArray=set.toArray(strArray);
     for(String str:strArray)//此处也可以使用for(int i=0;i<strArray.length;i++)
     {
         System.out.println(str);
     }
     //遍历集合的第二中方法,使用set集合直接遍历
     for(String str:set)
     {
         System.out.println(str);
     }
      
     //遍历集合的第三种方法,使用iterator迭代器的方法
     Iterator<String> iterator=set.iterator();
     while(iterator.hasNext())
     {
         System.out.println(iterator.next());
     }
}
  2,LinkHashSet
        LinkHashSet不仅是Set接口的子接口而且还是上面HashSet接口的子接口。
        TreeSet是通过TreeMap实现的。
        LinkHashSet底层是基于LinkedHashMap来实现,和HashSet主要区别在于LinkedHashSet中存储的元素是在哈希算法的基础上增加了链式表的结构。
  3,TreeSet
        TreeSet底层算法基于红黑树,允许null,有序,不重复,元素位置固定
        TreeSet和HashSet的区别:
            1,HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的
            2, Map的key和Set都有一个共同的特性就是集合的唯一性.TreeMap更是多了一个排序的功能.
            3, hashCode和equal()是HashMap用的, 因为无需排序所以只需要关注定位和唯一性即可.
                a. hashCode是用来计算hash值的,hash值是用来确定hash表索引的.
                b. hash表中的一个索引处存放的是一张链表, 所以还要通过equal方法循环比较链上的每一个对象才可以真正定位到键值对应的Entry.
                c. put时,如果hash表中没定位到,就在链表前加一个Entry,如果定位到了,则更换Entry中的value,并返回旧value
            4, 由于TreeMap需要排序,所以需要一个Comparator为键值进行大小比较.当然也是用Comparator定位的.
                a. Comparator可以在创建TreeMap时指定
                b. 如果创建时没有确定,那么就会使用key.compareTo()方法,这就要求key必须实现Comparable接口.
                c. TreeMap是使用Tree数据结构实现的,所以使用compare接口就可以完成定位了.
public static void main(String[] args) {
        //String实体类中实现Comparable接口,所以在初始化TreeSet的时候,
        //无需传入比较器
        TreeSet<String> treeSet=new TreeSet<String>();
        treeSet.add("d");
        treeSet.add("c");
        treeSet.add("b");
        treeSet.add("a");
        Iterator<String> iterator=treeSet.iterator();
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
}

五、Map接口

    Map中的每个成员方法由一个关键字(key)和一个值(value)构成。
    常见实现类HashMap、TreeMap、LinkedHashMap、HashTable
    1,HashMap
        HashMap无序的、不可重复、查询快、null、非线程安全。
        HashMap实现了Map、CloneMap、Serializable三个接口,并且继承自AbstractMap类。
        HashMap基于hash数组实现,若key的hash值相同则使用链表方式进行保存。
        HashMap遍历方式
public static void main(String[] args) {
        //方式1
        Map map = new HashMap();
        map.put("A", "1233");
        map.put("B", "12334");
        map.put("C", "12334");
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            System.out.println(entry.getKey() + "--" + entry.getValue());
        }
        //方式2效率高
        iter = map.keySet().iterator();  
        while (iter.hasNext()) {  
            Object key = iter.next();
            System.out.println(key + "--" + map.get(key));
        }  
    }
  2,TreeMap
        TreeMap有序的、不可重复、遍历快、允许null、非线程安全。
        HashMap基于红黑树实现。
    3,LinkedHashMap
        LinkedHashMap有序的、不可重复、遍历快、允许null、非线程安全。
        LinkedHashMap输出顺序和输入顺序相同。
        LinkedHashMap继承hashMap,底层存储结果是Hashmap的table
    4,Hashtable
        Hashtable有序的、不可重复、不允许null、线程安全。
原文地址:https://www.cnblogs.com/my-haohao/p/5659104.html