Anatomy of arrays and collection containers in Java

Source: Internet
Author: User

Storage containers commonly used in Java are collections of arrays, and each container is stored in a different form and structure.

Arrays are the most basic containers, and there are three ways to create an array, such as the following:

int[] arr = new INT[5];

int[] arr = new string[]{1,2,3,4,5};

Int[] arr = {1,2,3,4,5};

From the above three ways can be seen, in the definition of the array when there is a common feature is to be able to directly see the length of the array, which is also a major feature of the array, that is, the definition of the length of time, and once the definition of the length of the array can not be changed, which is the array in the late development of the Because there is a large uncertainty about the length of the container storage data in the actual development. In the way of defining an array, you can also see that when you define an array, you need to specify the type of the array, which is the type of data that can be stored within the array, so that the data types that can be stored cannot be changed after the array is defined, and there are some limitations.

Memory storage for Arrays:

Next look at the collection, the collection is in the development of more commonly used containers, first to look at the classification of the collection.

Set classification of two large categories, respectively, collection collection and map collection, collection is the use of single-value storage, and map collection is the use of key-value pairs of the form of storage, the following look at the collection of specific implementation of the storage structure and characteristics of the class.

Common features of the collection collection:

1. Stored elements can be of different types

2, the length of the set is variable, not fixed

3. You can iterate through the elements in the collection using an iterator iterator

ArrayList collection: The array structure used at the bottom of the storage, the elements stored in the array can be null, and can save multiple duplicate elements, stored in the order of ordering, each element corresponds to a foot tag value, in the operation of the elements can be done by the foot mark, At the time of the query, specify the script, directly get the specified element, no need to traverse the collection, so when the query efficiency is high, and delete efficiency is low, because in the process of adding and deleting the collection elements involved in the traversal.

LinkedList collection: At the bottom of the use of the list structure, symbolizing, there is also a foot mark, can be operated by the foot, the efficiency of the query is low, from the source analysis, the first to judge the foot in the middle of the left side or the right side, and then on the side of the set to traverse, query elements, When adding or deleting, the specified node is operated directly, without traversing and increasing efficiency.

Comparison of ArrayList and LinkedList:

ArrayList add element:

LinkedList adding elements:

Set collection: There is a gap between the stored data and the list collection, the list collection can store duplicate elements, and the set collection cannot store duplicate data, the data stored in the list collection is ordered, and the set is unordered and has no feet, and the elements in the collection cannot be manipulated by the foot tag.

HashSet collection: In the underlying storage is the hash table structure used, the stored data can be null, because unique, so only one can be stored, HashSet collection is how to ensure that the element is unique?

The HashSet bottom uses arrays and hashing algorithms that can be used to store data in multiple comparisons to achieve

Specific implementation:

1. hash algorithm for stored elements to get a hash value

2, will use this computed hash value to the structure of the stored data to find the corresponding position, when there is no element in the position to add directly

3. If an element exists at that location, the two elements are compared using the Equals method

4. If the newly added elements are not added to the collection, the hash value of the added element will be recalculated if not equal, and then added

TreeSet collection: A storage structure that uses a two-fork tree at the bottom, and HashSet is completely different from the mechanism of ensuring that the storage data is not duplicated

Specific implementation:

Method One: Using the default comparator comparable, this time need to store the object corresponding to the class implementation comparable interface, and re-CompareTo method, in the method to write the specific guarantee element unique implementation code, if not implemented and rewritten in the storage of elements will occur when the exception

Mode two: The use of a custom comparator comparator, at this time the specific storage object without implementing the comparator interface, you can customize a class, the class implementation comparator interface, and override the Compare method, when creating TreeSet collection, A constructor that passes the object of the custom class as a parameter to the TreeSet collection. But this way will need to write a class, more cumbersome, the implementation is not friendly, this time you can consider the use of anonymous internal classes.

Two ways of implementation will not conflict, this may have doubts about beginners, Java since the design, of course, there will be no conflict, read the source of the comparison mechanism of the design of the clever praise.

When creating a TreeSet collection, you can either use the parameterless constructor or use a parameterized constructor (the parameter is the comparer), set a variable (comparator) to hold the comparer in TreeSet, and invoke a comparison method when storing the element. In this method, the comparator is judged, if the comparator is not empty, it means that using the constructor method to pass the comparator, we use the incoming comparator to compare the uniqueness of the elements, instead, the default comparator is used to compare the uniqueness of the elements.

Map collection: The map collection storage element uses the form of a key-value pair, the key is unique, and the value can be not unique, the key and value can be null, a key can only correspond to one value, but a value can correspond to multiple keys, key-value pairs in the Map collection is treated as an object, a key value pair as an object, The Map collection stores multiple key-value pairs of objects. The map collection does not have an iterator similar to the collection collection when traversing the element, but instead uses the Keyset method or EntrySet method to first move to the set set and then iterate with the iterator.

HashMap collection: HashMap is also a hash table mechanism, in the guarantee of key uniqueness operation and hashset the same.

TreeMap Collection: The TreeMap adopts a two-fork tree structure, which is identical to treeset in the operation of guaranteeing the uniqueness of the key.

Add:

1. The elements stored in an array can be objects or basic data types, and collections can only store objects

2. The collection collection can be iterated using the iterator iterator, and the list collection has its own iterator Listiterator, which applies to the list collection, and other collections under collection cannot be used

3, the use of ArrayList, HashSet, HashMap collection in the development process is more common, other collections use less

4, LinkedList API has AddFirst, Removefirst, AddLast, Removelast four unique methods, so you can use the collection to simulate the stack and queue structure

Anatomy of arrays and collection containers 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.