A container is something that is used to hold multiple objects. It's strictly a reference to the Save object. Because the actual data of the object is placed in another place. The container is just an identifier that points to the area of memory. The array and other containers differ mainly in three: efficiency, type, and the ability to store basic types. An array in Java is the most efficient way to store and randomly access an object reference sequence. It's just a simple linear sequence, so the access is very fast. But it also loses other features. When an array is created, its length is fixed. Typically, a fixed-size array is created and, when space is low, Create a larger array, and then move all references in the old array to the new array. This mechanism is used by another container in Java: ArrayList. Therefore, the efficiency of the ArrayList is lower than the array. There are other containers in Java: List,set ( Each object holds only one copy, and Map (allows one object to be associated with another object). They are all handled against object. Object is the base class for all classes in Java, so these containers can hold all Java classes. Note: Basic types such as: Boolean char Byte short long float double void. object is not inherited, so these containers cannot hold these. If you want to put it in, the wrapper classes that must be provided in Java for these classes are the corresponding wrapper classes: BooleanCharacterByte ShortIntegerLongFloatDoubleVoid because the base type is not inherited from object, casting is not possible. You must use a wrapper class for the cast. For example, to cast a string to an int, it must be: Integer.praseint ("3"); Must be used in the wrapper class. Automatic boxing and automatic unpacking principle After packing the basic type of packaging can be casually put in the container.
Disadvantages of Java containers: 1 When you save an object to a container, its type information is lost. Because the container is object-oriented. So you can put different types of objects into the same container. And arrays are different, and an array can only hold objects of the same type. 2 A type conversion must be made when the object is used from the container. Of course, the above problem can be solved by generics.
Selection of containers:There are actually only three kinds of containers: Map, List, Set; But each interface has a different implementation version. Their differences can be summed up by what is behind them. In other words, the interface you use is implemented by what data structure. List selection:For example: ArrayList and LinkedList both implement the list interface. Therefore, whichever you choose, the basic operation is the same. But ArrayList is provided with the underlying support by the array. and LinkedList is implemented by a two-way list. So, LinkedList is better if you want to frequently insert or delete data into the list. Otherwise, you should use a faster ArrayList. selection of SetHashSet is always better than treeset. The reason for the existence of the latter is that it can maintain the sort state of the elements. Therefore, if you need a ordered set, you should use the TreeSet map selection: Ditto, as far as possible to choose HashMap, Use TreeMap only if you need to arrange a good sequence of maps;
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