* ** Collections class
Public Static<T> List <t> ncopies (
IntN, t o) Fill the collection with the reference of a single object
Public Static<T>
VoidFill (list <?
SuperT> list, t OBJ) {Replace the elements that already exist in the list. You cannot add new elements.
* ** All collection sub-types of the filling container have constructors that accept another collection object and use the elements in the receiving object to fill the new container.
* ** Optional operations to perform different addition and removal methods are optional in the collection interface, which means that the implementation class does not need to provide function definitions for these methods. The optional operation declares that calling some methods will not execute meaningful behaviors, but will throw an exception. Define an optional operation objective: to prevent interface explosion in the design. It enables Java container class libraries to be easy to learn and use. Unsupported operations are special cases that can be implemented when necessary. But to make it work in this way: 1 unsupportedoperationexception must be a rare event. 2. If an operation is not supported, unsupportedoperationexception may occur when the interface is implemented, rather than sending the product program to the customer. It indicates a programming error: Incorrect interface implementation is used. The most common unsupported operations come from containers supported by a fixed-size data structure. 1. Use arrays.
AslistWhen converting an array to a list
2Collections.
Unmodifiablelist(
NewArraylist <string> () is an unchangeable container. For methods that accept the container as a parameter, this document specifies the optional methods that must be implemented.
* ** List functions are commonly used: add an object and get an element at a time. iterator obtains the iterator used for this sequence.
* ** Set interface: unique element. It does not guarantee the maintenance of the element order. hashset: the query speed is fast. The stored element must define hashcode () treeset: keep the order, and the bottom layer is the tree structure. It can be used to extract ordered sequences from the set. The element must implement the comparable interface into hashset: The speed with hashset, and the internal use of the linked list to maintain the order of elements (insertion order), The hashcode () sortedset must be defined: sort elements by object comparison functions
* ** In addition to concurrency, the queue implementation includes the queue list and priorityqueue. the difference lies in sorting behavior rather than performance. You can insert an element from a queue and retrieve it from the other end. Except for the priority queue, the queue is FIFO. Priority queue: You can store objects that implement the comparable interface and determine the priority of Objects Based on compareto () to control the storage sequence of elements. Bidirectional queue: You can add or remove elements to or from any queue. The Java class library does not have interfaces for Bidirectional queues. However, the consumer list contains two-way queue methods, which can be used in combination to implement the deque <t> class, but is not commonly used.
* ** The basic idea of a map ing table (associated array) is that it maintains key-value associations, so you can use keys to find values. Linear search in the ing table is slow. Hashmap uses hash codes for fast query. Hash Codes are the int values of each object (hashcode () in the object class ()). MAP is implemented based on the hash. The overhead of inserting and querying "key-value pairs" is fixed. You can use the constructor to set the Capacity and load factor to adjust the container performance.
Linkedhashmap: similar to hashmap, but when traversing it iteratively, the order in which "key-value pairs" are obtained is the insertion order, or the least recently used (LRU) Order. It is only a little slower than hashmap. It is faster during iterative access because it uses the linked list to maintain the internal order.
Treemap: based on the data structure of the red and black trees. When you view "keys" or "key-value pairs", they are sorted (the order is determined by comparabel or comparator ). Treemap features that the results you get are sorted. Treemap is the only map with submap () method. It can return a subtree.
Weakhashmap: weak Key Map. objects used in map are also allowed to be released. This is designed to solve special problems. If a reference other than map points to a key, the key can be recycled by the garbage collector.
Identifyhashmap: Use = to replace the hash map used by equals () to compare the "key. Designed to solve special problems.
Any key must have equals (). If the key is used for hash map, it must have an appropriate hashcode (). If the key is used for treemap, it must implement comparable.
* ** Default object for hash and hash codes. equals () Only compares the object address. The default hashcode () uses the object address to calculate the hash code. Therefore, one groundhog (3) is not equal to the other groundhog (3 ). Therefore, if you want to use your own class as the key of hashmap, you must reload hashcode () and equals () at the same time. The correct equals () must meet the following conditions: 1 Self-inverse. Returns true 2 symmetry for any X, X. Equals (X. For any X, Y. If X. Equals (y) = true, then Y. Equals (x) = true 3 is passed. For any X, Y, Z. if X. equals (y) = true, Y. equals (z) = true then X. equals (z) = true 4 consistency. For any X, Y. If the information used for the equivalent comparison in the object is not changed, the result will be consistent no matter how many times X. Equals (Y) is called. 5. Return false for any X, X. Equals (null) that is not null.
Hash is used to search for another object. The key is speed, and the key must be quickly searched for to increase the speed. The purpose of hashing is to store the key in a location that can be quickly searched. It uses an array to store index values (because the array size is fixed, data cannot be directly stored, and hashcode cannot be directly stored. Therefore, an array of a fixed size is used to store index values, hashcode % size = index), each index value corresponds to a list, and the key and value are stored in this list. In application, first locate the index value and then perform linear query on the corresponding list. This process is slow, but a good hash algorithm will make the values evenly distributed in the list. Custom hashcode (): The important principle is that a specific object will generate a specific hashcode value no matter when hashcode () is called. Otherwise, hashcode is different in put () and get, the object cannot be found. Sometimes we do not want each new object to generate different hashcode (which is generated by the object by default). Instead, we want to determine whether to generate a new hashcode by checking whether the values in the object are the same. Therefore, we need to customize the method.
* ** Select an appropriate container list: arraylist should be selected by default. when there are a large number of insert/delete operations that affect performance, you can consider using the sort list (which implements a two-way linked list. Each object contains the reference of one object and the next object in the list, therefore, it is suitable to insert or delete data in the list ). If the number of elements is fixed, you can use arrays. aslist () to obtain a fixed list or use array directly. Set: by default, hashset is selected, and its add () and contains () are fast. treeset (whose iterate () is fast) is selected only when the set needs to be sorted ). Linkedhashset add () consumes more time than hashset, because it has additional costs to maintain the same list as the hash container. MAP: the preferred hashmap, so the speed is faster. Select treemap when frequent sorting is required. In addition to identityhashmap, other map insertion operations will become slower as map increases. Hashtable and hashmap have the same speed. Because hashmap must replace hashtable, the underlying implementation principle is the same. Linkedhashmap is slightly slower to insert because it needs to retain the insertion sequence, so its iteration speed is faster. Hashmap parameter: initial capacity. If many entries are stored, increase the initialization capacity to avoid frequent rehash. Load Factor: When this threshold is reached, the capacity will double and rehash. 0.75 is the discount value. List sorting and searching: If comparator is used for Array sorting, the same comparator must be used for searching. Read-Only collections map list <string> A = collections. Unmodifiablelist( NewArraylist <string> ( Data)); Unmodifiablecollection Unmodifiableset Unmodifiablesortedset Unmodifiablemap Unmodifiablesortedmap With these methods, private writable containers can be provided inside the class, while read-only containers can be provided externally. The synchronized collections map syntax is the same as that of collection <string> C = collections. Synchronizedcollection( NewArraylist <string> ());
Fail fast: the Java container has the Fail fast mechanism to prevent operations on containers in multiple places at the same time. If someone else modifies the container during use, an exception is thrown: concurrentmodification-exception. The concurrenthashmap, copyonwritearraylist, and copyonwritearrayset use techniques that avoid concurrentmodificationtions
Reference: In Java The. Lang. Ref library makes GC more flexible. To retain a reference to an object but allow it to be recycled by GC, you can use reference to reference the object, including softreference, weakreference, and phantomreference. Softreference is used for memory-sensitive high-speed caching. SoftReferenceThe principle is to ensure that all soft references are cleared before the JVM reports memory insufficiency when the object is referenced. WeakReferenceA typical use of a class is canonicalized mapping ). In addition, for objects with relatively long lifetime and Low Re-creation overhead, weak references are also useful, and their memory will be recycled no matter whether the current memory space is sufficient or not. PhantomReferenceClass can only be used to track the upcoming collection of referenced objects. Virtual references, as the name implies, are just the same as virtual ones. Different from other types of references, virtual references do not determine the object lifecycle. If an object only holds a virtual reference, it is the same as no reference and may be recycled by the garbage collector at any time.