Java: Container/Collection (Map (HASHMAP,TREEMAP))

Source: Internet
Author: User

HASHMAP:

*map Interface: Store data in a way that is a key-value pair. Disordered
* Common Implementation classes:
*--hashmap: The implementation of a hash table-based MAP interface.
* Common methods of construction:
*hashmap () constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
*hashmap (int initialcapacity) constructs an empty HashMap with the specified initial capacity and default load factor (0.75)
* Common methods:
*put (K key, V value) associates the specified value with the specified key in this mapping.
* Get (Object key) returns the value mapped by the specified key, or null if the map does not contain any mapping relationships for that key.
* Size () returns the number of key-value mapping relationships in this map.
* Remove (Object key) Removes the mapping relationship for the specified key from this mapping, if any.
* Clear () Removes all mapping relationships from this mapping.
* ContainsKey (Object key) returns True if this map contains a mapping relationship for the specified key.
* Containsvalue (Object value) returns True if this mapping maps one or more keys to a specified value.
*----Linkedhashmap
*--hashtable
*--treemap

import Java.util.hashmap;import java.util.Map; Public classTesthashmap { Public Static voidMain (string[] args) {Map<String,String> map =NewHashmap<string,string>(); //add elements to the container: store them as key-value pairs.Map.put ("Jack","Jackie Chan"); Map.put ("Jay","Jay Chou"); Map.put ("Eason","Eason Chan");//adding elements//Take://map.remove ("Eason");//removing elements by keySystem. out. println ("whether to include Eason:"+map.containskey ("Eason")); System. out. println ("Whether it contains Jackie Chan:"+map.containsvalue ("Jackie Chan")); System. out. println ("the container has a total of"+map.size () +"a key-value pair element"); String name= map.Get("Jack");//gets the value of an element based on keySystem. out. println (name); String name2= map.Get("Jay"); System. out. println (name2); }}

There are two ways to HashMap values:

import java.util.hashmap;import java.util.map;import java.util.map.entry;import java.util.Set;/** *MAP Traversal * set<k> KeySet () returns a Set view of the keys contained in this map.  * set<map.entry<k,v>> EntrySet () returns the Set view of the mappings contained by this map. */ Public classTESTHASHMAP2 { Public Static voidMain (string[] args) {Map<String,String> map =NewHashmap<string,string>(); //add elements to the container: store them as key-value pairs.Map.put ("Jack","Jackie Chan"); Map.put ("Jay","Jay Chou"); Map.put ("Eason","Eason Chan");//adding elementsMap.put ("Jack","Jack");//if the key repeats, the overwrite will occurSystem. out. println (map); //Method 1: Get the collection of keys first, get the value by keySet<string> KeySet =Map.keyset ();  for(String key:keyset) {String value= map.Get(key); System. out. println (key+"----"+value); } System. out. println ("-------------------------------"); //Method 2: Gets a collection of key-value pairs, and then gets the key and value from the key-value pair. (recommended)//entry<string,string>: Generic,set<entry<string,string>> of keys and values for Entry (key-value pairs) in set setsSet<entry<string,string>> EntrySet =Map.entryset ();  for(Entry<string, string>Entry:entryset) {String key= Entry.getkey ();//gets the key for the key-value pairString value = Entry.getvalue ();//get the value of a key-value pairSystem. out. println (key+"****"+value); }    }}

Java: Container/Collection (Map (HASHMAP,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.