Java Container Classes

Source: Internet
Author: User

Javacontainer list, ArrayList, vector and map, HashTable, HashMap distinctionthe difference between list and ArrayListlist->abstractlist->arraylist(1) List is an interface that ArrayListis a concrete class that implements the list interface .       They are father-son relationship, we often use ArrayList, but the reference to the common list to operate ArrayList      This is a simple interface-oriented programming, such as: List myList = new ArrayList ();(2)They are mainly used to save the object's collection, remember is to save the object of Oh, you can biography an int (class) go in (3)to remove objects stored in it, you can use subscripts, such as:Object AAA = mylist.get (0);       so we took out the first object that we saved in MyList and gave it toSecond, detailed      looks like the list and map are all interfaces .      cannot be instantiated.      I used to write list list = new Vector ();      now write the list list = new ArrayList ();      using ArrayList instead of vector because the former has better performance than the latter;      But two of them are all made up of list excuses.      Same as map map = new HashTable ();(previous)map map = new HashMap ();(now)ArrayListand HashMap are asynchronous, vectors and Hashtable are synchronous, so vectors and hashtable are thread-safe, and ArrayList and HashMap are not thread-safe. Because synchronization takes machine time, vectors and Hashtable perform less efficiently than ArrayList and HashMap. Collection├list│├linkedlist│├arraylist│└vector│└stack└setMap├hashtable├hashmap└weakhashmapListInterfaceThe list is an ordered collection, using this interface to precisely control where each element is inserted . The user is able to access the elements in the list using an index (where the element is positioned in the list, similar to an array subscript), similar to an array of java. Unlike the set mentioned below, the list allows the same elements. in addition to having collection interface Prerequisitesiterator ()method , list also provides a Listiterator ()method , returns a Listiteratorinterface , compared to the standard iterator interface, Listiterator has a number of add () methods that allow adding, deleting, setting elements, and traversing forward or backward. the common classes that implement the list interface are Linkedlist,arraylist,vector and stacks. ArrayListclassArrayList implements a variable-size array. It allows all elements, including null. ArrayList is not synchronized. size, the Isempty,get,set method runs at constant time. But the Add method cost is the allocated constant, and adding n elements requires an O (n) time. Other methods run at a linear time. each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion efficiency. like LinkedList, ArrayList is also unsynchronized (unsynchronized). MapInterfaceNote that map does not inherit the collection interface, and map provides a key-to-value mapping. A map cannot contain the same key, and each key can only map one value. The map interface provides views of 3 collections, and the contents of the map can be treated as a set of key sets, a set of value collections, or a set of key-value mappings. HashMapclassHashMap and Hashtable are similar, the difference is thatHashMapis non-synchronous and allows NULL, that is, null value and null key. , but when HashMap is treated as collection (the values () method can return collection), its iteration sub-operation time overhead is proportional to the capacity of HashMap. Therefore, if the performance of the iterative operation is quite important, do not set the initialization capacity of the hashmap too high or load factor too low. 1.Listis an interface, the list attribute is ordered and ensures that the elements are preserved in a certain order.ArrayListis its implementation class, which is a list implemented with arrays.Mapis an interface, the map attribute is to find objects based on an object.HashMapis its implementation class, HashMap with a hash table implementation of the 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 type of the list, you only need to:List List = new LinkedList ();//linkedlistis also a list of the implementation of the class, but also ArrayList brothersIn this way, there is no need to modify other code, which is the elegance of interface programming.Another example of this is the following declaration in the method of the class:private void Domyaction (List list) {}This method can handle all the classes that implement the list interface, and implement generic functions to some extent.3.You can customize your custom class by implementing List,map (or collection) if you feel that Arraylist,hashmap performance does not meet your needs at the time of development.

Java Container Classes

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.