A collection of Java comparisons----MAP

Source: Internet
Author: User

Java collection Another large interface is the map, and collection The biggest difference is that the collection store is an object, and map storage is a pair of objects, that is, the form of key-value storage, key cannot be duplicated. There are 4 classes that can instantiate a map: Hashmap,hashtable,weakhashmap,treemap.

The traversal of map is the first to get his set set through EntrySet (), similarly, KeySet () Gets the set set of his key, values () gets the set set of his value, and iterates through the set collection of iterator.

HashMap

He has all the attributes of map, both key and value can be null. is not thread-safe, if the key is repeated, then the value of the last key will overwrite the value of the previous key, anyway the saved collection, key will not have duplicates. He has an initial capacity (16) and a load factor (default is 0.75), he has a threshold (initial capacity * load factor), and if size exceeds this threshold, then the capacity is X 2.

Hashtable

He feels like HashMap, Hashtable's initial capacity is 11, and each size increases x2 + 1. Both key and value cannot be null for his thread-safe. His traversal sequence and HashMap are reversed, HashMap is the former backwards, and Hashtable is from the back forward. He has a way of traversing, enumeration.

TreeMap

It feels like he's a key with a sequential set of Key-value, and his key can't be null,value can. Nor is he thread-safe.

Weakhashmap

He is similar to HashMap, but his keys are weak keys and will be recycled by GC.

Finally, if you want to turn a thread-unsafe collection into a secure The Collections.synchronizedmap,collections.synchronizedset,collections.synchronizedlist function is a workaround.

A collection of Java comparisons----MAP

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.