Collections and Arrays in Java

Source: Internet
Author: User

The difference between an array and a set: (1) The array is fixed in size, and the same array can only hold data of the same type (base type/reference type) (2) The Java collection can store and manipulate a set of data that is not fixed. (3) If the program does not know exactly how many objects need to automatically expand capacity when the space is insufficient, you need to use the Container class library, array does not apply. Contact: Use the corresponding toarray () and Arrays.aslist () methods to recall the conversion. I. Architecture of the collection: list, set, and map are the three most important interfaces in this set system. The list and set inherit from the collection interface. The map also belongs to the collection system, but differs from the collection interface. Set does not allow elements to be duplicated. HashSet and TreeSet are the two main implementation classes. Set can only be evaluated by a cursor, and the value cannot be duplicated. The list is ordered and allows elements to be duplicated. ArrayList, LinkedList, and vectors are the three main implementation classes. ArrayList is thread insecure, Vector is thread-safe, these two classes are implemented by an array of LinkedList is thread insecure, the underlying is the list implementation of the MAP is a key-value pair collection. Where the key column is a collection, key cannot be duplicated, but value can be repeated. HashMap, TreeMap, and Hashtable are the three main implementation classes of map.   HashTable is thread-safe and cannot store null values HASHMAP is not thread safe and can store a null value of two. The difference between list and ArrayList 1.List is an interface, and the list attribute is ordered, ensuring that elements are preserved in a certain order.   ArrayList is its implementation class, which is a list implemented with arrays.   Map is an interface, and the map attribute is to find objects based on an object. HashMap is its implementation class, HashMap is implemented with a hash table map, is to use the object's Hashcode (Hashcode () is the method of object) for fast hash lookup.   (for hash lookup, see << data structure >>)   2. In general, if not necessary, the recommended code only deals with the List,map interface.   For example: List list = new ArrayList (); The reason for this is that the list is equivalent to a generic implementation, and if you want to change the list type, you only need to: List List = new LinkedList ();//linkedlIST is also the implementation class of the list, but also the ArrayList brothers class, so there is no need to modify other code, this is the elegance of interface programming.   Another example is that in the class method, the following is declared: private void Domyaction (List list) {} This method can handle all classes that implement the List interface, to some extent, implement generic functions. 3. If you feel that Arraylist,hashmap performance does not meet your needs when developing, you can customize your custom class by implementing List,map (or collection).

Collections and Arrays in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.