List, Set, map three interfaces, when accessing elements, what are the characteristics of each

Source: Internet
Author: User

First, the list and set have similarities, both of which are a collection of single-column elements, inheriting the common parent interface, Collection.

The List indicates a sequential collection, noting that it is not sort by age, by size, by price, and so on. When we call the Add (OBJ-E) method multiple times, each time the object is added is like a train station to buy tickets in the same order as the queue, sorted by first served. Sometimes, you can also jump in the queue, that is, by calling the Add (int index,obj e) method, you can specify where the current object is stored in the collection. An object can be repeatedly stored in the list, each call to the Add method, the object is inserted into the collection once, in fact, not the object itself is stored in the collection, but in the collection with an index variable point to the object, when the object is add multiple times, That is, there are multiple indexes in the collection pointing to this object, as shown in X. List In addition to the iterator interface can get all the elements, and then iterate through each element, you can also call get (index i) to clarify the number of the first.

The set does not allow repeating elements, so-called repetition, that is, there cannot be two equal (note, not just the same) object, that is, assuming that the set set has an A object, now I want to put a set set of a B object, but the B object is equal to equals of the A object, The B object is not stored, so the Add method of the Set collection has a Boolean return value, when there is no element in the collection, when the Add method can successfully join the element, it returns True when the collection contains an element equal to the equals of an element. The Add method cannot join the element at this point, and the return result is false. When set takes an element, it is impossible to say the first number of elements, only the iterator interface to get all the element, and then iterate through each element.

Unlike list and set, Map is a collection of two columns, with the Put method defined as follows: Put (obj key,obj value), each time you store a pair of key/value, you cannot store duplicate keys, This repeating rule is also equal to equals. You can obtain the corresponding value according to key, which is the value that the get (Object key) returns to key. In addition, you can get all the key combinations, you can also get all the value of the union, you can also get a combination of key and value of the Map.entry object collection.

A List of single-column data sets that hold elements in a particular order (in order) and can have repeating elements. Set simple interest data collection, internal ordering, cannot have duplicate elements. Map Double column data collection, save Key-value value, no order, key cannot be duplicated, value can be multivalued.

HashSet is stored according to the hashcode value, rather than directly by the size of the hashcode value. For example, "ABC"--->, "def"--->, "xyz"---> 65 the Order of storage in HashSet is not 62,65,78. HashSet are stored in the order in which they are inserted, what does the Hashcode method of the stored object do? Think of the!hashset collection to compare two objects for equality, first to see if the Hashcode method is equal, and then to see if the Equals method is equal. New two student are inserted into the hashset, look at the size of the hashset, implement the Hashcode and Equals methods, and then look at size.

The same object can be added multiple times in the vector. Add elements to the collection, which is equivalent to a string connected to the target object. To HashSet but not to add many times.

List, Set, map three interfaces, when accessing elements, what are the characteristics of each

Related Article

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.