Java Fourth Week study diary

Source: Internet
Author: User

Day01

Two-column collection:

In real life, some of the data are mapped relationships exist, that is, the existence of pairs, such as couples. A singleton collection cannot exhibit a mapping relationship, so it learns a double-column collection.

A double-column collection has no iterators.

1.Map

Two-column collection:

———————— | Map if it is the implementation of the Map class interface collection class, has the characteristics: the stored data are in the form of key-value pairs exist, the key is not repeatable, the value can be repeated.

———————————— | The Hashmap is also based on a hash table.

———————————— | Treemap

———————————— | Hashtable

1-1 HashMap

The implementation of a hash table-based map interface.

Storage principle of HashMap:

When adding elements to HashMap, the Hashcode method is first called to get the hash value of the element, and then the operation is calculated to calculate the location of the element in the hash table.

Case one: If no element is stored so far, the element can be added directly to the hash table.

Case two: If the calculated position currently has other elements, then the Equals method of the element is also called to compare with the element at this location, if equals returns false, then the element is allowed to be stored, if the Equals method returns True, Then the element is treated as a repeating element and is not allowed to be stored.

Iteration

Way One keyset

Way two: Values

Way three: EntrySet

Storage principle of HashMap

1-2 TreeMap

TreeMap is also based on the red-black tree (binary tree) data structure implementation, characteristics: The key of the element will be sorted storage.

TreeMap things to be aware of:

1. When adding elements to TreeMap, if the keys of the elements are in a natural order, they are sorted by the natural order characteristics of the keys.

2. When adding elements to TreeMap, if the key of the element does not have a natural order, then the class to which the key belongs must implement the comparable interface, and the key comparison rule is defined on the CompareTo method.

3. When adding elements to TreeMap, if the key itself does not have a natural order, and the class to which the key belongs does not implement the comparable interface, the comparer must be created when the TreeMap object is created.

Java Fourth Week study diary

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.