1. The difference between array and other containers is that it can hold privitive objects with high efficiency. Try to use arrays first. Array copy system. arraycopy (). This copy is a shortest copy (that is, for the object array, it copies the reference array. 2. The arrays class has four static methods: whether the array is equal to equals (); filling the array fill (); sorting the array sort (); search for the binarysearch () array in an sorted array. In addition, there is an aslist () method. 3. There are two ways to compare array elements: 1) Implement the java. Lang. Comparable interface. It only has one method compareto (). This method can receive an object as a parameter. If an existing object is smaller than the parameter object, a negative number is returned. If it is equal, 0 is returned. If it is greater than the parameter object, a positive number is returned. You only need to implement this interface method, and this class can successfully perform equals, sort, and other relatively large operations. 2) These classes do not implement the comparable interface. We cannot forcibly rewrite these classes to add the compareto method. We can implement another interface: comparator to solve the problem. This interface has two methods: Compare and equals. Equals generally does not require special implementation, mainly compare. In this way, classes and comparison methods are completely separated. (There is a significant difference from the previous method) for example: the class string has a custom compare method, and the sorting of "A", "B", and "C" will be ACB, it may not be the ABC we expected. In this case, we can: Import Java. util. *; public class arraystest {public static void main (string [] ARGs) {// todo auto-generated method stub string [] S = {"A", "B ", "C"}; arrays. sort (s, new AC (); system. out. println (arrays. aslist (s) ;}} class AC implements comparator {public int compare (Object obj1, object obj2) {string S1 = (string) obj1; string S2 = (string) obj2; return s1.tolowercase (). compareto (s2.tol Owercase () ;}} 4. You must pay attention to the following issue during search: the search of arrays is meaningful only after sort is used. However, in this case, even after sort, the search is meaningless: for example, the string compare comparator is temporarily specified, rather than the string class itself, just like the above routine. Because the comparator used by binarysearch can only be the default comparator of the class. For example, "A", "B", and "C" can be arranged in ABC format, but B cannot be found. The solution is to add the third parameter of binarysearch, that is, specify the comparator to use sort () of the same routine (). At the same time, there are five ABCDE strings. If "1" is found,-1 is returned. The meaning of this-1 is as follows: 1 should be inserted in the first position, but it is not found. 5. The Container class is a complicated class in Java. The container class should solve the problem of "how to hold objects. But it divides this problem into two categories: 1) Collection: A group of independent elements with certain regularity. It can be divided into list and set. 2) map: A group of pairs in the form of "key-value. Essentially, it is also a collection. Its key can be extracted to be set, its value can also be extracted to collection, and of course the pair itself can also be extracted to set. Map can be easily expanded to multiple dimensions. Set the value to map. When selecting different container classes, pay attention to the following two main problems: repeatability and insertion sequence. 6. the array tool is in arrays, while the collection tool is in collections. 7. Container disadvantages-you do not know the object type. For example, if you have defined dog and pig, you can put them in the same container. However, you must force type conversion and use the try keyword during extraction. But sometimes, even if you do not know the object type, it can be used normally. For example, the tostring () method. 8. The benefit of the iterator is that it provides a decoupling mechanism that allows you to traverse the set without any difference, regardless of the specific implementation of the Set. This is a design pattern. Use the iterator as much as possible. For example, some set types are accessed by subscript, and some cannot be accessed by subscript. If the set type needs to be changed, the corresponding code must be modified. If you keep using the iterator, you generally only need to change the variable definition. 9. The classification chart of the container categorization container should be reviewed over and over again. This figure is very important. Interfaces related to object storage are mainly collection (set, list) and map. Ideally, we should only deal with these interfaces. In most cases, this is enough. For example, we should define: List x = new writable list (); note that the interface is defined first. This definition method can easily implement container transformation. For example, change x = new arraylist () without disturbing other code. This is the elegance of this method. From these examples, we can see that a number has a list and no set as the lower mark (so it is unordered and does not support random access), and a map as the object's lower mark. 10. List is ordered. It stores elements in a certain order and can perform addition, deletion, and other operations in the list (not suitable for arraylist, because of slow speed ). Listiterator objects can be created for Bidirectional traversal. There are actually two types of list: arraylist: Good at random access. Shortlist: A linked list. It is good at insert and delete operations and is suitable for sequential access. Can be used as stacks, queues, and bidirectional queues. 11. The set interface is identical to the Collection interface, and the elements in the Set interface are unordered. Each element added to the set is unique. Therefore, the equals method must be implemented for each set element (object. There are three implementations: 1) hashset. This is the set implemented to optimize the query. It has no other meaning, but it is just faster to query. The insertion sequence is not saved. 2) treeset. Is an ordered set. In this way, an ordered sequence can be extracted from the set. The order here is not the index order like list, but the order of the elements themselves. This is useful in sorting sets. 3) define hashset. It has the advantages of hashset and saves the insertion sequence. In this way, the iterator will be used to repeat the time, And a sequence will be obtained in the order of insertion. You can obtain the class name as follows: for example, instance is a treeset. Instance. getclass (): Get class COM. p9pip. util. treeset instance. getclass (). getname (): Get: COM. p9pip. util. treeset instance. getclass (). getname (). replaceall ("// W + //. "," "): Obtain the treeset. Replaceall uses a regular expression! If you implement it yourself, you have to write a function. 12. sortedset (the base class of treeset) has some separate methods for sorting implementation: 1) comparator (): Return comparator. If the object is used, null is returned. 2) object first (): returns the smallest element. 3) Object last (): returns the largest element. 4) sortedset subset (fromelement, toelement): returns the subset of the set. The elements are from fromelement to toelement (not including toelement ). 5) sortedset headset (toelement): returns all elements smaller than toelement. 6) sortedset tailset (fromelement): returns all elements greater than or equal to fromelement. Treeset only inherits the sortedset abstract class ...... 13. From a more abstract point of view, both arraylist and map are objects of the same type. They are searched by subscript. The subscript of the former is a number, and the latter is an object. At the same time, the former is less efficient, but the occupied space is relatively small. The latter is more efficient, but occupies a large space. The difference between map classes is efficiency, the order of holding and representing objects pair, the length of holding objects, and how to determine the equality of keys. There are mainly the following map classes: 1) hashmap: map is implemented using the hash algorithm. This class is used to replace hashtable. The constructor can provide capacity and load factory. 2) linkedhashmap: it is like hashmap, But it combines the characteristics of the linked list. In this way, the performance of regular queries is slightly lower than that of hashmap, and the use of iterator is much higher than that of hashmap. 3) treemap: implemented based on the data structure of the red and black trees. An ordered map implementation. It has the submap method to obtain a subtree. Therefore, the element must implement the comparable interface or provide a comparator. 4) weakhashmap and identityhashmap: not commonly used. Note: The primitive Wrapper class (such as integer) cannot be used for situations where map values may change ). Once these values are assigned, they can only be read (think about the methods in integer and other classes ). The corresponding classes must be implemented separately. 14. sortedmap (base class of treemap) is very similar to sortedset, including methods. 15. hashtable, vector, and stack belong to Classes left behind by the old version 16. The difference between implementing containers and containers lies in the data structure implementation behind the corresponding interfaces. 16.1 how to select list 1) process a fixed number of elements and use arrays; 2) process a large number of inserts and deletions; 3) process sequential reads and use arraylist. Generally, arraylist is used first. Once a large number of insert and delete operations are found, you can change it to the partition list directly. The performance of both is quite different. 16.2 it is enough to select a set and select a hashset. Hashset must be used to speed up traversal. To be sorted, use treeset. These three features are very similar. 16.3 it is enough to select hashmap for map selection. Select treemap for sorting. For linkedhashmap, it is generally not used. These three features are very similar.
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