Summary of Java Collection learning:

Source: Internet
Author: User

Attach a picture (your own painting is ugly also say not all, down a beautiful point of view):

Specific each interface and implementation of what the meaning of the class is not to say, rookie a may also be said not too clear, this article mainly said some of my study in the process of impressive problems.

1, Vector, ArrayList, LinkedList contact and difference:

The only difference between vector and ArrayList is that the vector thread is safe and ArrayList is not, so without regard to thread safety, of course, the use of ArrayList, although the method is the same, but the vector performance is slightly weaker;

As for ArrayList and LinkedList, the former uses the array structure, the latter uses the linked list structure, then the actual use of the two is obvious, fast random access must be ArrayList good, and a lot of insertions, deletions is linkedlist good at things. It is important to note that each ArrayList has a capacity, which of course can be automatically increased, but it can improve efficiency by calling a ensurecapacity before making a lot of data additions, and the party will certainly not forget that.

Another: LinkedList also non-thread-safe Oh, there is no synchronization method provided, but can be created in the list when the new synchronization out, presumably so

List List = Collections.synchronizedlist (new LinkedList ())

ArrayList ... should be the same.

2. Stack and vector

The diagram shows that the stack inherits from the vector, that is, the basic structure is the same, there are five additional methods to make the stack is different from the vector data structure: Push, pop, peek (get the top element of the stack), empty (test stack is empty), Search (detects the position of an element in the stack).

3, HashMap

HashMap through the hash value of the element to find, inserting, deleting and positioning elements in the map, no doubt HashMap as the first choice, then the problem, do not exclude the two elements of the hash value of the same Ah, then how to store it? Already has the cognition is: Originally a hashmap should be a similar to the structure of the array, when the hash value will have conflict at the same time, then the same hash value of the elements in the form of a linked list stored in the so-called bucket (bucket), so that the understanding is relatively vague, so began a web search "no Return", The results are shared with everyone:

HashMap conflict-handling issues

    1 ), Open address method  
         Open Address law is a record of those conflicts, using the hi= (key) +di Modn method to determine the hash address again.  
        N: Long for hash table;  
       DI: For the increment sequence, the following three kinds of ways are available:  
        1) linear detection re-hashing       di= c * I  
        2) Two-time probing and hashing      di = 12,- -22,  ,
        3) random detection and re-hashing       Di is a set of pseudo-random numbers or DI=IXH2 (key) (also known as double-hash function detection)  
   For example, a hash table with a table length of 11 has a record with the keyword 17,60,29 in it, and H (key) =key mod   11,        Existing 4th record, whose key word is 38  

H (=38) MOD 11=5 clash
h1= (5+1) MOD 11=6 clash
H2= (5+2) MOD 11=7 clash
h3= (5+3) MOD 11=8 not conflict
The method for other incremental sequences is also calculated as such.


2 ), chain address method
All records with the same hash address are linked in the same linked list. That is, when there is only one entry in each bucket in the HashMap, no conflict occurs, HashMap is an array that can be quickly found entry according to the index, but when a conflict occurs, a entry chain is stored in a single bucket, The system must traverse each entry sequentially until it is found. To reduce the data traversal, the elements of the conflict are inserted directly behind the first entry, so the entry, which was first put into the bucket, is at the very end of the entry chain. As can be seen from the put (Kkey,vvalue), the newly placed entry is always in the bucket in the case where the entry chain is stored in the same bucket.

Note: The above two paragraphs are derived from CV Dafa

In fact, there are many ways to deal with the conflict is not listed (the truth is not understood), it turns out that the simplest is the most easily remembered.

Also want to say a weakhashmap, actually not weak, should say is stronger, think here side key implement "weak reference", if a key is no longer referenced by external, then the key can be GC recycling, hum, know "garbage collection mechanism" of the bad bar.

4, TreeMap

If you want to traverse the map by nature or by your own sort, find treemap, because the element you add to the SortedMap implementation class must implement the comparable interface, otherwise you must give its constructor an implementation of the comparator interface. The TreeMap class is the only implementation of SORTEDMAP. Here with a small section of code to let everyone feel at will, this is the meaning, the details can be ignored

map<user> sortbyusername =new treemap<> (

New Comparator<user> () {//object defined as an instance of an anonymous inner class

@Override

Public int Compare (Item user1, user user2) {

// specific comparison method custom return values { -1,0,1}

}

});

If there is such a demand, Jiangzi is also very cool.

The above content as a summary or record it, later if there are other discoveries will continue to add, content will certainly have some inappropriate places, I hope you crossing take care of a Java rookie of the small heart ...

Summary of Java Collection learning:

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.