Collection list set map difference memory

Source: Internet
Author: User

Http://gwshuai.javaeye.com/blog/99373

These represent the set in Java. Here, we mainly look at whether its elements are ordered and whether they can be repeat for different memory for proper use. Of course, there are still synchronization differences. See the previous article.Article.

Http://tb.blog.csdn.net/TrackBack.aspx? Postid = 584112

The list interface is used to expand the collection. Its specific implementation classes are commonly used arraylist and javaslist. You can put everything in a list container and retrieve it as needed. Arraylist can be seen from its name that it is stored in an array-like form, so its random access speed is extremely fast, and the internal implementation of the arraylist is a linked list, it is suitable for frequent insert and delete operations in the center of the linked list. You can select an application as needed. The iterator mentioned above can only traverse the container forward, while the listiterator inherits the iterator idea and provides a bidirectional Traversal method for the list.

the set interface is also an extension of collection, but different from the list, the object elements in the set cannot be repeated, that is to say, you cannot put the same thing into the same set container twice. Its common implementations include the hashset and treeset classes. Hashset can be used to quickly locate an element. However, you need to implement the hashcode () method for objects in the hashset, it uses the algorithm of the hash code mentioned earlier. Treeset stores the elements in the Set in order, which requires that the objects in the set are sortable. This uses the other two aggregation classes comparable and comparator provided by the set framework. A class can be sorted, and it should implement the comparable interface. If multiple classes have the same sorting algorithm, you do not need to define the same Sorting Algorithm for each class. You only need to implement the comparator interface. There are two useful public classes in the Collection framework: collections and arrays. Collections provides some useful methods for sorting, copying, searching, and filling a collection container. arrays performs similar operations on an array.

Map is a container that associates key objects and value objects, and a value object can be a map, and so on. In this way, a multi-level ing can be formed. For key objects, similar to set, key objects in a map container are not allowed to be repeated to maintain consistency of the search results. If there are two key objects, the problem occurs when you want to get the value object corresponding to the key object. Maybe what you get is not the value object you want, and the result will be messy, therefore, the uniqueness of keys is very important and also conforms to the set nature. Of course, during use, the value object corresponding to a key may change. At this time, the modified value object will correspond to the key. There is no uniqueness requirement for value objects. You can map any number of keys to a value object without any problems (however, it may cause inconvenience to your use, you don't know whether you get the value object corresponding to that key ). MAP has two common implementations: hashmap and treemap. Hashmap also uses the hash algorithm to quickly find a key. treemap stores the key in order, so it has some extended methods, such as firstkey (), lastkey (), etc. You can also specify a range from the treemap to obtain its submap. The association between keys and values is very simple. You can associate a key with a value object using the pub (Object key, object Value) method. You can use get (Object key) to obtain the value object corresponding to this key object.

Http://zhidao.baidu.com/question/16113509.html

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.