Collections+iterator Interface | Map+hashmap+hashtable+treemap |

Source: Internet
Author: User
Tags comparable sorts

Collections+iterator interface

1. Collections is a tool class that operates sets, lists, and maps, and collections provides a number of methods for sorting, querying, and modifying collection elements, as well as providing immutable Synchronization control for collection objects sort operations: Reverse (List): Reverses the order of the elements in the list Shuffle (list): Randomly sorts a list collection element sort (list): Specifies the list based on the natural order of the elements The collection elements are sorted in ascending order sort (list,comparator): Sorts the List collection elements according to the sequence specified by Comparator swap (list,int, int): Swaps the I and J elements in the specified list collection 2. The Iterator interface Iterator interface is primarily used to traverse the elements in the Collection collection, and the Iterator object is also known as an iterator Iterator interface that hides the underlying details of various Collection implementation classes, providing the application with traversal The unified programming interface of the Collection collection element Iterator is used only to traverse the collection, and Iterator itself does not provide the ability to load objects. If you need to create a Iterator object, you must have a collection that is iterated. You can also traverse an element using for (person p:persons) {}

Map+hashmap+hashtable+treemap

1. Map is used to store data with mappings, so there are two sets of values stored in the map collection, one for saving the key in the map, and the other for storing the key and value in the Value,map in the map to be any reference type of data

The key in map does not allow repetition, that is, any two keys of the same map object are returned false by the Equals method comparison

There is a one-way relationship between key and value, that is, a unique, deterministic value can always be found through the specified key.

2. HashMap and Hashtable

HashMap and Hashtable are the two typical implementation classes of the Map interface

Difference:

Hashtable is an ancient Map implementation class and is not recommended for use

Hashtable is a thread-safe MAP implementation, but HASHMAP is thread insecure.

Hashtable does not allow NULL as key and value, while HashMap can

As with the HashSet collection, the order of the elements is not guaranteed, and Hashtable, HASHMAP cannot guarantee the order of the key-value pairs.

Hashtable, HashMap to determine the two keys equal to the standard is: Two keys are returned by the Equals method True,hashcode value is also equal.

Hashtable, HashMap The criterion of two value equals is: Two value returns true through the Equals method

3.TreeMap

TREEMAP Store Key-value pairs, you need to sort the key-value pairs according to Key. TreeMap can guarantee that all key-value are in an orderly state.

Sort of Key for TreeMap:

Natural sort: All keys of TREEMAP must implement the comparable interface, and all keys should be objects of the same class, otherwise they will be thrown classscastexception

Custom sort: When you create a TreeMap, you pass in a Comparator object that is responsible for sorting all the keys in the TreeMap. The comparable interface is not required for the Key of the Map at this time

Collections+iterator Interface | Map+hashmap+hashtable+treemap |

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.