Java collection------MAP (HashMap implementation)

Source: Internet
Author: User
Tags set set

package java_util_map;import java.util.collection;import java.util.hashmap;import  java.util.map;import java.util.map.entry;import java.util.set;public class maptest01  {Public static void main (String[] args)  {/* * map is an interface, HashMap is an implementation class of map  */Map<String, String> map = new HashMap<String, String> ();/* The  * put () method adds key-value to the map  */map.put ("0",  "Spring"), Map.put ("1",  "Summer"), Map.put (" 2 ", " Autumn ") Map.put (" 3 ", " Winter "), put in/* * hashmap, if the added key value is repeated, the method returns the old value value  */ System.out.println (Map.put ("4",  "Seasons"));/* *  print the Map object directly, you can view the ToString () method HashMap SYSTEM.OUT.PRINTLN (map);/* * containskey () returns whether it contains the specified key value  */system.out.println (Map.containskey ("2" ));//return truesystem.out.println (Map.containskey ("one"));//return false/* * containsvalue () returns whether the specified value value is included  */systEm.out.println (Map.containsvalue ("Autumn"));//return truesystem.out.println (Map.containsvalue ("222"));// Return false/* * keyset () returns a set set of all key components  */for  (string key :  Map.keyset ())  {system.out.println (key+ "--" +map.get (key) + " ");} /* * isempty () Determines whether the map is empty  */system.out.println ("is the map collection empty?").    "+map.isempty ()");/* * getordefault (Key,defaultvalue) Returns the value of the specified key, if there is no key value, Returns DEFAULVALUE&NBSP;*/SYSTEM.OUT.PRINTLN (Map.getordefault ("1",  "2"));//return summer  System.out.println (Map.getordefault ("One",  "2"));//return 2/* * size (): Returns the number of key-value pairs  */system.out.println (Map.size ());//return 5/* * values (): Returns the collection collection of value in the map  *  @return  : Spring Summer Autumn Winter Seasons */Collection< String>  values = map.values ();for  (string value : values)  { System.out.print (value +  "  ");} /* * replace (K key, v value): Replaces the value values with the old Value */system.out.println () of the specified key; Map.replace ("2",  "2");for  (String key : map.keyset ())  {system.out.println (key+ "  --> "+ map.get (key));} /* * replace (K key, v oldvalue, v newvalue): Replaces the NewValue value with the old oldValue of the specified key  *  @return: replaces the entry for the specified key only if  currently mapped to the specified value. */system.out.println ("^^ ^^ ^^ ^^ ^^ ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ "), Map.replace (" 2 ", " 2 ", " Autumn "),//return truemap.replace (" 2 ", " 1 ",   "Autumn");//return false,because the oldvalue is not correctfor (String  key : map.keyset ()) {System.out.println (key+ "-and" +map.get (key));} /* * remove (Object key): Removes the key-value */system.out.println of the specified key ("^^ ^^ ^^ ^^ ^^ ^^ ^^^^ ^^ ^^ ^^ ^^ ^^ ^ "), Map.Remove (" 0 ");for  (String key : map.keyset ())  {system.out.println ( key+ "--" +map.get (key)); /* * putifabsent (k key, v value): If the specified key is not   * already associated with a value  (or is mapped to  null)  associates it with the given value and returns null,  * else returns the current value. */system.out.println ("^^ ^^ ^^ ^^ ^^ ^^, ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^ "), Map.put (" 5 ",  null), Map.putifabsent (" 5 ", " Spring ");for  (string key :  Map.keyset ())  {system.out.println (key+ "-and" +map.get (key));} /* * entryset (): Returns the set set of Key-value in the map, and the set collection is unordered and non-repeatable key-value * returns a  set view of the mappings contained in this map. */ System.out.println (^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^"); Set<map.entry<string, string>> mapset = map.entryset ();for  (Entry< String, string> entry : mapset)  {system.out.println (entry);}}


This article is from the "11941149" blog, please be sure to keep this source http://11951149.blog.51cto.com/11941149/1845731

Java collection------MAP (HashMap implementation)

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.