Java-set sorting, queue, hash map and how to traverse, java-map

Source: Internet
Author: User

Java-set sorting, queue, hash map and how to traverse, java-map

1.1 set sorting
You can use the static method sort of java. util. Collections in the collection tool class to sort the List only because it is ordered.
Collections. sort (list );
Sort string: the string is sorted by the encoding order of the string characters.
If you want to sort strings in the desired order, you can customize the element type to implement the Comparable interface to override the int compareTo (Point o) method.
The method that needs to be rewritten after the comparable interface is implemented. this method is used to define the size relationship between the current object (this) and the parameter object (o). The return value does not focus on the specific values, value range:
When the return value is greater than 0: The current object is greater than the parameter object (this> 0)
When the return value is <0: The current object is smaller than the parameter object
When return value = 0: two objects are equal
1.2 Queue java. util. Queue
A queue can store a group of elements. The access element must follow the following principle: FIFO: first input first output first-in-first-out queue interface integration from collection
Method:
Boolean offer (E) joins the queue to add the specified element to the end of the queue.
E poll () is an out-of-the-queue operation. The first element is extracted and deleted from the queue.
E peek () references the first element to obtain the first element, but the element is still in the queue.
The Deque and Deque queues inherit from the Queue interface. The dual-end Queue is the Queue that both ends can access.
Method:
Boolean offer (E) joins the queue to add the specified element to the end of the queue.
Boolean offerFirst (E) to add the given element to the first of the Team, as shown in the preceding figure.
Boolean offerLast (E) is used to add the specified element to the end of the queue.
E poll () is an out-of-the-queue operation. The first element is extracted and deleted from the queue.
E pollLast () team operation, retrieves the first element, and then deletes the element from the queue.
E peek () references the first element to obtain the first element, but the element is still in the queue.
2. Hash Map: the java. util. MapMap lookup table stores elements in the form of key-value pairs. So it looks like a multi-row table.
Map requires that duplicate keys are not allowed (equals is true)
Common Implementation class java. util. HashMap hash list, map implemented by the hash algorithm
V put (K k, V v) adds the given kv to the map. If the given key already exists in the map, because the key must not be repeated, therefore, this is to replace the value operation. The return value is the original value corresponding to the key. If the key does not exist in the map, null is returned. The same key is used to replace the value operation.
V get (K k) obtains the corresponding value based on the given key. If the given key does not exist in the current map, null is returned.
Remove (K k) deletes an element based on the key
3. One of the factors that affect the new capabilities of hash query is that the linked list is generated and the linked list is generated mainly:
The hashcode value of the element as the key is the same, while equals is not true.
When the hashcode value is the same, the position of the array inside the map is the same, but if the key is different, a linked list will be generated at this location. The data retrieved from the traversal chain table will reduce the hashmap retrieval performance.
So avoid
Therefore, the rewrite requirements for equals and hashcode are also described in the api documentation:
1. Rewrite in pairs, that is, when you override the equals method of a class, you should rewrite the hashcode method together
2. consistency: when the two objects equals are compared to true, the numbers returned by the hashcode method should be equal. Otherwise, the two objects have the same hashcode, and the equals method should be true. Otherwise, a linked list will be generated in the hashmap.
3. Stability: when the value of the attribute of an object involved in the equals comparison does not change, the number returned by calling hashcode multiple times should not change.
Another factor that affects the query speed of a hash table is that the size of the hash table is 16 by default, and is expanded whenever the hash table capacity reaches 75%, however, because the position of the hash element is calculated based on the hashcode value, when the capacity changes, and the elements in the hash list can still be found, the original elements need to be re-traversed, that is, when the hash is expanded, additional operations are generated. To solve this problem, you need to consider the hash capacity.
Map Traversal
There are three ways to traverse map
1. traverse all keys
Traverses all keysets <K> heyset (); stores all the keys in the current map into a set and returns the result. traversing This set is equivalent to traversing all the keys in the map.
2. traverse all key-value pairs (entries)
Obtain each key-value pair set <entry> entrySet () map. Saving each key-value pair as an entry is an internal class of map, provides two common methods: getkey () getvalue ()
3. traverse all values (not commonly used)
Traverse all valueCollection values () and save all values in the current map to one set.

 

I am a beginner. If any updates are poor, I would like to thank you!

More highlights will be updated later!

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.