1.MAP Overview
Map and collection exist to hold data that has a mapping relationship : Key-value
both the key and value in the MAP can be data of any reference typethe key in the map is stored with set and does not allow repetition , so the set type in map needs to override the Hashcode () and Equals () methods, because the reference object is the two methods to guarantee the uniqueness of the set. There is a one-way relationship between key and value in map, that is, a unique, deterministic value can always be found through the specified key.
Is the inheritance tree of the map system, the most commonly used is hashmap,linkedhashmap to maintain the order of adding the map with a list of links, the order of traversal is consistent with the order of addition.
The main methods in 2.Map
Package Org.tizen.moipservice;import Java.util.collection;import Java.util.hashmap;import java.util.Iterator; Import Java.util.map;import java.util.set;import org.junit.test;/* * Object put (object Key,object value) Add a key to the Map and Valueobject remove (Object key) to delete the established key and the corresponding value, that is, to delete the key-value to void Putall (Map t) void clear () object Get (object key) Gets the value of the key set, such as none, returns Nullboolean ContainsKey (Object key), Boolean Containsvalue (object value) int size () Boolean IsEmpty () Boolean equals (Object obj) */public class Testmain {@Testpublic void Test1 () {Map m = new HashMap (); M.put ( "123a", M.put ("B", 98), M.put ("AbC", 1221); M.put ("B", 101); M.put ("AA", 101); System.out.println (M.size ()); System.out.println (m); System.out.println (M.remove ("123a"))//remove returns the object to be deleted, if not, returns NULLSYSTEM.OUT.PRINTLN (m); Integer i = (integer) m.get ("B"); System.out.println (i); Boolean B = M.containsvalue (101);//Determine if there is a value with the same value, Here value corresponds to the type that needs to be overridden by the Equals method System.out.println (b);} /* * Meta View operation method: Set KeySet () Collection values () set EntrySet()/@Testpublic void Test2 () {Map m = new HashMap (); M.put ("123a", "98"), M.put ("B", ""); M.put ("AbC", 1221); M.put (null,1);// 1. Get map KeySet, traverse set S = M.keyset (); SYSTEM.OUT.PRINTLN ("traversal set"); for (Object o:s) {System.out.println (o);} 2. Traverse valuescollection C = m.values (); SYSTEM.OUT.PRINTLN ("Enhanced for loop traversal values"); for (Object o:c) {System.out.println (o);} SYSTEM.OUT.PRINTLN ("Iterator traversal Values"), Iterator i = C.iterator (), while (I.hasnext ()) {System.out.println (I.next ());} 3. Traverse Key-value to set S1 = M.entryset (); for (Object o:s1) {map.entry Entry = (map.entry) o; System.out.println (Entry.getkey () + "" +entry.getvalue ());}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JAVA--DAY13--------Map using