Java Collection Class learning record

Source: Internet
Author: User

Attributes marked as transient are not saved when the object is serialized
Int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = arrays.copyof (arr1, new_length);//arrays is an array of tool classes
ArrayList's ToArray method of converting to a static array is this principle
The ArrayList expansion principle is also the arrays.copyof () method
A new array was created
Define an array int[] a={3,1,4,2,5}; Int[] B=a; Array B is just another reference to group A, which is a shallow copy.
If you change the value of an element in array B, it is actually changing the value of the element of array a
To achieve deep replication, you can use clone or system.arraycopy
As the following code

1 int[] a={3,1,4,2,5};
2 int[] B=a.clone ();
3 b[0]=10;
4 System.out.println (b[0]+ " " +a[0]);
Output is 10 3
Visible changes the value of B, but does not change the value of the element of a


In addition to not synchronizing and allowing Nulls, the HashMap class is roughly the same as Hashtable. HashMap is not thread-safe, and if you want thread-safe hashmap, you can synchronizedmap get thread-safe HashMap by collections The static method of the class.

Map map = Collections.synchronizedmap (new HashMap ());

In the Java programming language, the most basic structure is two, one is an array, the other is an analog pointer (reference), all the data structures can be
Constructed with these two basic structures, HashMap is no exception. HashMap is actually a "chain-table hash" of the data structure, that is, arrays and chains
The combination of the table. It has a fairly fast query speed primarily because it calculates the location of the store by calculating the hash code.

HashMap is mainly through the key hashcode to calculate the hash value, as long as hashcode the same, the calculated hash value is the same. If you store more objects, it is possible that different objects will have the same hash value, and there is the so-called hash conflict. The students who have learned the data structure know that there are many ways to solve the hash conflict, hashmap the bottom is to solve the hash conflict through the list.


When we put the element in the HashMap, we first recalculate the hash value according to the Key's hashcode, which is worth the element in the array according to the hash.
Position (i.e. subscript), if the array has other elements already in place, then the elements in this position will be stored in the form of a linked list,
The new join is placed in the chain head, the first to join the end of the chain. If the array has no elements at that position, the element is placed directly in that position in this array
On

HashMap is actually a entry array, entry object contains the key and the value, where next is also a entry object, it is used to deal with the hash conflict, form a linked list.

The loadfactor load factor is the degree to which the elements in the Hsah table are filled,

A balance and compromise must be found between "opportunity for Conflict" and "space utilization". This balance and tradeoff is essentially a balance and tradeoff between the well-known "time-space" contradictions in the data structure.

If the machine memory is sufficient and you want to increase the query speed, you can set the load factor to a smaller point, but if the machine memory is tight and there is no requirement for the query speed, you can set the load factor a bit larger. But generally we do not have to set it, let it take the default value of 0.75 is good.

If "key is null", the key-value pair is added to table[0].
In the HashMap, the method of h& (length-1) is used instead of the modulus, the same uniform hash is achieved, but the efficiency is much higher, which is also an improvement of HashMap to Hashtable.
Length is 2 n times, is even, length-1 is odd, last one is 1, (H is hash value)

Length takes an integer power of 2 so that the probability of collisions of different hash values is small, so that the elements can be uniformly hashed in the hash table.
If these two Entry keys return true by equals, the newly added Entry value overrides the Entry value in the collection, but the key is not overwritten. If these two Entry keys return false by Equals, the newly added Entry will form a Entry chain with Entry in the collection, and the newly added Entry is located in the head of the Entry chain.
When there are more and more elements in the HashMap, the probability of hash collisions becomes higher, because the length of the array is fixed. Therefore, in order to improve the efficiency of the query, it is necessary to expand the array of HashMap, array expansion This operation will also appear in the ArrayList, this is a common operation, and after the HashMap array expansion, The most performance-consuming point arises: the data in the original array must recalculate its position in the new array and put it in, which is resize.
expansion is required for array replication, copying arrays is a very performance-intensive operation, so if we have predicted the number of elements in HashMap, then the number of preset elements can effectively improve the performance of HashMap

LinkedList the underlying data structure is based on a two-way cyclic linked list, and the head node does not store the information, as follows:

linkedlist<string> list = new linkedlist<string> (); 2         list.add ("First"), 3         list.add ("Second"), 4         list.add ("Thrid"), 5        System.out.println (list); 6         listiterator<string> ITR = List.listiterator (); 7         while (Itr.hasnext ()) {8            System.out.println (Itr.next ()); 9        }

Java Collection Class learning record

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.