java-Collection (iv)

Source: Internet
Author: User
Tags set set

The Map Collection features: The Set stores key-value pairs, a pair of pairs in the memory, and the keys are unique. Features: To ensure the uniqueness of the keys in the map set. Map:hashtable Bottom is a hash table data structure, can not be stored null key null value, the collection is thread synchronization HashMap the underlying is the hash table data structure, allowing the use of NULL values and NULL keys, is not synchronized, high efficiency TreeMap Bottom The layer is a two-fork tree structure, with threads that are not synchronized and can be used to sort the keys in the map collection. 1, Add. Put (Key,value): When the stored key is the same, the new value replaces the old value and returns the old value. Returns NULL if the key is not duplicated. void Putall (MAP); 2, delete. void Clear (): Empty value Remove (key): Deletes the specified key. 3, Judge. Boolean IsEmpty (): Boolean ContainsKey (Key): Whether it contains a key Boolean Containsvalue (value): Contains value 4 and is removed. int size (): Returns the length of value get (key): Gets the corresponding value by specifying the key.         If NULL is returned, you can tell that the key does not exist. Of course there is a special case, that is, in the HashMap collection, the null key can be stored in the null value. Collection values (): Gets all the values in the Map collection. 5, want to get all the elements in the map: principle: There is no iterator in map, collection has an iterator, as long as the map collection into a set set, you can use an iterator. The reason why the set is transferred is because the map set has the uniqueness of the key, in fact, the set set is derived from the Map,set set at the bottom of its practical is the map method.  The method of turning map set into set:set<k> KeySet (); Saves all the keys in the map to the set collection. Set<k,value> EntrySet ();//The mapping of keys and values. Entry is the internal interface in the map interface; Why should it be defined inside a map? Entry is the access to the key-value relationship, the gateway to the map, and the access to the key-value pairs in the map. ---------------------------------------------------------Remove all the elements in the map collection by one: KeySet () method. The keys in the map collection can be removed and stored in the set collection. To SET collection for iteration. The iteration is completed, and the obtained key is obtained by the Get method. Set KeySet = Map.keyset (); Iterator it = Keyset.iterator ();      while (It.hasnext ()) {Object key = It.next ();       Object value = Map.get (key); System.out.println (key+ ":" +value);} --------------------------------------------------------take out all the elements in the map collection by two: EntrySet () method. set<map.entry<string,string>> EntrySet = Map.entryset (); Iterator<map.entry<string,string>> it = Entryset.iterator (); while (It.hasnext ()) {Map.entry me = ( Map.entry) It.next (); System.out.println (Me.getkey () + "::::" +me.getvalue ());

Java-Collection (iv)

Related Article

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.