Java collection----Map, Collection_java

Source: Internet
Author: User

Interface: red; implementation class: Black font

One, Collection Collection

Collection

|_____Set(hashset)

| |_____sortedset(TreeSet)

|_____List(arraylist,linkedlist,vector)

Collection: The root interface in the collection hierarchy, the JDK does not provide an implementation class for this interface.

List: Ordered (in the order in which they are placed), can be repeated, with subscript.

Set: unordered, not repeatable, no subscript.

SortedSet: Is the sub-interface of the set interface, and the elements in the SortedSet are ordered (in alphabetical order).

By means of the CompareTo method of comparable interface, the elements are ordered. The elements that are placed must implement the comparable interface (or accepted by the specified comparator).

List The difference between the implementation classes of the interface :

ArrayList : is essentially an array. Thread is not secure. Query (get/set) fast, additions and deletions (add/remove) slow.

LinkedList: Essentially a two-way linked list. Thread is not secure. Query (Get/set) slow, additions and deletions (add/remove) fast.

vector : Almost exactly the same as ArrayList, the only difference is that the vector is a synchronous class (synchronized) and is thread-safe.

Second, Map Collection

Map (hashmap,hashtable)

|_____sortedmap(TREEMAP)

Map: Stored key-value pairs, cannot contain duplicate key, can have duplicate value.

sortedmap: The sub-interface of the map SortedMap is a map that arranges keys in ascending order.

HashMap , Hashtable and TreeMap the Difference

HashMap : Thread is not secure. Both key and value can be null. The elements are unordered. The bottom is the hash table data structure.

Hashtable: Thread safe. Both key and value cannot be null. The elements are unordered. The bottom is the hash table data structure.

TreeMap: Thread is not secure. Both key and value cannot be null. The elements are ordered (in ascending alphabetical order). The bottom is a two-fork tree data structure.

Third, Map of the Key , Set How to implement the value of not repeating

Map put the same Key , after the overwrite of the

Map map=new HashMap ();

Map.put ("name", "John");

Map.put ("name", "Harry");------valid, overwriting previous

Set when the same element is placed, the first one is valid and the following is not placed

Set set=new hashset ();

Set.add ("a");---effective

Set.add ("222");----judgment has been made and will not be put into

1 , HashMap of the put with HashSet of the Add

Because the Add () method of the HashSet is actually converted to the put () method of the call HashMap to add the Key-value pair when the collection element is added. The put () method of the HashMap first invokes the. hashcode () to determine that the return value is equal, if the return value is equal, and then returns true with the Equals comparison, it is finally assumed that the key object is equal and already exists in HashMap.

2 , TreeMap of the put with TreeSet of the Add

When the Put method is invoked in TreeMap to add a key value, its compareTo (or Compare) method of calling the object is compared to all keys, and the method is considered equal if the meal returns 0.

TreeSet when adding elements, call the CompareTo or compare method to position the element, that is, return CompareTo or compare return 0 that is considered the same position element, that is, the same element

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.