java-Test start Map basic operation

Source: Internet
Author: User

Package Java_test;import Java.util.arraylist;import Java.util.collections;import java.util.comparator;import Java.util.hashmap;import java.util.list;import java.util.map;import Java.util.set;import java.util.TreeMap;public Class Mapdemo {public void MapAction () {//map basic operation map<string,string> Map =new hashmap<string,string> (); Map.put ("A", "This is a");//Add a bunch of key-value pairs int len=map.size () to the map,//How many pairs of key-value pairs System.out.println (len);//Output 1String A=map.get ("a");//Gets the value of key a System.out.println (a);//Outputs this is aSystem.out.println (Map.get ("B"));//Gets the value of a non-existent key, The return value is null and does not throw an exception map.put ("A", "This is the second a");//Add an already existing key, and the value added later overrides the previously existing VALUESYSTEM.OUT.PRINTLN ( Map.get ("a"));//Output This is the second Aboolean c=map.containskey ("a");//Determine if there is a key aSystem.out.println (c) in the map;// Output Trueboolean V=map.containsvalue ("This is the second a");//Determine if there is a given valueSystem.out.println (v) in the map;//Output truemap< String,string> m1=new hashmap<string,string> () m1.put ("A", "This is the third a"); M1.put ("D "," This is D "); SYSTEM.OUT.PRINTLN (M1); Map.putall (M1);//Take map and M1, if the M1 already exists in the map of the key, then the value in M1 will overwrite the value of the existing key in the map, The value of the set is assigned to MAPSYSTEM.OUT.PRINTLN (map);} public void Loopmap () {map<string,string> map=new hashmap<string,string> (); Map.put ("A", "This is a"); Map.put ("B", "This is B");/** * Set can be understood as a special list, but the elements inside the object is not allowed to be duplicated, keyset is to put the key into a set of * HashMap is unordered arrangement of keyset, The Set object is also unordered */set<string> set=map.keyset (); for (String S:set) {System.out.println (Map.get (s));//Loop output map Value} }public void Loopremovemap () {map<integer,string> map=new hashmap<integer,string> (); Map.put (1, "This is a" ); Map.put (2, "This is B");/** * Set can be thought of as a special list, but the set does not have a way to take a value by get in the form of a list * to value, you can convert set to list, or to iterator */set <Integer> set =map.keyset (); List<integer> list=new arraylist<integer> (set),//set to listfor (int i=0;i<list.size (); i++) { Map.Remove (List.get (i)); SYSTEM.OUT.PRINTLN (map);//output {}}public void Clearmap () {//empty mapmap<integer,string> map=new Hashmap<integeR,string> (); Map.put (1, "This was a"); Map.put (2, "This is B"); Boolean e =map.isempty ();// Determine if the map has a key value to System.out.println (e);//output truemap.clear ();//Clears all the key values in the map to E=map.isempty (); System.out.println (e);//output true}public void Sortmapbykey () {//simple sort map<string,string> map=new hashmap<string ,string> (); Map.put ("A", "This is a"), Map.put ("C", "This is C"), Map.put ("B", "This is B"); SYSTEM.OUT.PRINTLN (map);/** * TreeMap is a map implementation class by Key for the holy-market arrangement * TreeMap automatically sorts the key-value pairs inside * Use this to convert HashMap to TreeMap, You can implement the MAP by key to sort */map<string,string> tm=new treemap<string,string> (); Tm.putall (map); map=tm; SYSTEM.OUT.PRINTLN (map);} public void Sortfuzamapbykey () {map<string,string> map=new hashmap<string,string> (); Map.put ("A", "this is A "); Map.put (" C "," This is C "); Map.put (" B "," This is B ");/** * Lonkedhashmap will record the order in which you put it, and the output will be output in the order in which you put it * using this, Sort the HashMap key by the required number, then put in a linkedhashmap to achieve a complex ordering of the Map */map<string,string> lm= new hashmap<string,string > (); List<string> List=new arraylist<string> (Map.keyset ()); Collections.sort (List,new comparator<string>) () {}}public static void main (string[] args) {Mapdemo m=new MapDemo ( ); M.mapaction (); M.loopmap (); M.loopremovemap (); M.sortmapbykey ();}}

  

java-Test start Map basic operation

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.