Programming Fundamentals Series - the - Discussion List, Set, Map and generic (a) - single collection

Before learning Java generics, collections of understanding has been vague, over time, generics and collections have some simple cognitive, we intend to consolidate write about, I hope you can point to understand Gangster inappropriate, very grateful! ! !

In the Java language, the collection is divided into two systems: Collection and Map collections, these two systems is the biggest difference is that --Collection separate collection Map collection is a double row

There are two interfaces --List and Set under a separate collection, there are a lot of implementation class in the List interface, we use the List and Set time is through their implementation class is instantiated, the more common implementation class should be the ArrayList LinkedList collections and collections

Briefly about the difference between these two collections, ArrayList collection is done by the underlying array, the array realization is characterized by: Find fast, slow additions and deletions , LinkedList collection is achieved through the linked list, the list of features are additions and deletions fast, slow look .

 

This set is two things in common: Both implementations can guarantee its storage and retrieval of the order is the same.

public  class ListDemo01 {
     / * List set of features:
        Ordered: consistent sequence of storage elements and removed
        Can be repeated: a storage element may be repeated
     * / 
    Public  static  void main (String [] args) {
         // create the collection object 
        List <String> List = new new the ArrayList <String> ();

        // add elements 
        list.add ( "the Hello" );
        list.add("java");
        list.add("word");
        List.add ( "Word" );
         // output collection object
 //         System.out.println (list);
         // iterator method set list traverse 
        the Iterator <String> IT = list.iterator ();
         the while (it.hasNext ())
        {
            System.out.println(it.next());
        }

    }
}

The result is this:

More commonly used class that implements the interface Set and HashSet is LinkedHashSet

We can see from the name, Set collection is achieved through the Hash table, Hash table features is to ensure that data can not be repetitive, that Set collection is not stored repeating elements. By understanding the storage Hansh we will know, by Hash to store data, we deposit into the data read out of order and the order data is inconsistent. This is also a difference between the interface and the Set List interface.

import java.util.HashSet;
import java.util.Set;

/*
Set the feature set:
    Collection does not contain duplicate elements
    Method is not indexed, it can not be used for ordinary loop iterates
 * / 
Public  class SetDemo {
     public  static  void main (String [] args) {
         // create the collection object 
        the Set <String> SET = new new HashSet <String> ();

        // add elements 
        set.add ( "the Hello" );
        set.add("world");
        set.add("Java");

        // traverse 
        for (String S: the SET)
        {
            System.out.println(s);
        }
    }
}

The result is this:

但凡事也会有例外,在Set集合下有一个实现类是可以保证插入顺序与检索顺序一致的,这个实现类就是LinkedHashSet,为什么它能呢?

它的底层是通过链表加Hash来实现的,Hash保证数据的不重复性,链表保证数据插入检索的一致性

import java.util.LinkedHashSet;

/*
LinkedHashSet是由哈希表跟链表来实现Set集合的,又因为Set是不能重复的和存储读取不一致的,所以,这个是对它的一个改进,不能存重复值,是由Hash表来保证的,同时又能进行
存储和读取一致,这是由链表来保证的
 */
public class LinkedHashSetDemo {
    public static void main(String[] args) {
        //创建集合对象
        LinkedHashSet<String> LinkHashSet = new LinkedHashSet<String>();

        //添加元素
        LinkHashSet.add("hello");
        LinkHashSet.add("word");
        LinkHashSet.add("Java");

        //遍历结合
        for (String s : LinkHashSet)
        {
            System.out.println(s);
        }
    }
}

运行结果:

这就是我对于单列集合的理解,总结一下就是:Collection下有List和Set接口,这俩接口下最常用的实现类是ArrayList集合和LinkedList、HashSet和LinkedHashSet,其中这里面list的两个实现类中ArrayList是数组实现,便于查找,LinkedList用链表实现,便于插入删除。Set的两个实现类中HashSet是通过Hash表来实现的,能保证数据的不重复性,但是不能保证数据插入检索顺序一致。LinkedHashSet既能保证数据不重复,又能保证插入检索一致。

Guess you like

Origin www.cnblogs.com/Xiaomoml/p/11961147.html