("0", MAP1); Map.put ("1", map); = Map.get ("1"); = = map); System.out.println (Neemap.get ("0"));Returns the result: True and {a=a}Description:map can add itself as a key or value to itself .
Query operation: Object get (Object key)//If not found, returns Null;boolean ContainsKey (object key); Boolean Containsvalue (object value); int Size (); Boolean isEmpty ();
View operation: Set KeySet ()//Returns the view set of all keys; Collection values ()//Returns the view se
Way one This is the most common and in most cases also the most desirable way to traverse. Used when the key value is required.
1234
MapnewHashMapfor(Map.EntrySystem.out.println("Key = " + entry.getKey() + ", Value = "+ entry.getValue()); }
Method Two traverses the keys or values in the For-each loop.If you only need the keys or values in the map, you can implement the traversal through keyset or values instead of using EntryS
10 Simple Java Performance optimizations:1. Using StringBuilder2. Avoid using regular expressionsWith regular expressions, you should at least cache the pattern to avoid compiling the pattern repeatedly.Static final Pattern Heavy_regex =Pattern.compile (((((*y) *z) * ");3. Do not use the iterator () method4. Do not call the high overhead method5. Using the original type and stackStored on the heapInteger i = 817598;Stored on the stackint i = 817598;6. Avoid recursion7. Use
over the elements of the map being passed in addition to the algorithm that the iteration put () runs to add each key-value pair to the map. It should be noted, however, that Putall () correctly adjusts the size of the map before it joins all elements, so if you do not adjust the size of the map yourself (which we will introduce), Putall () may be more efficient than expected. View MapThe elements in the iteration Map do not have a straightforward method. Suppose you want to query a map to see
coercion of type conversions. It is often common in a collection frame. When the type of data being manipulated is undefined, the object class is earlier defined to extend, and now defines generics to extend.Generics can be defined on classes, methods, and interfaces, and for static methods in a class, the generics defined on the class cannot be accessed because the type of the generic is determined when the object is initialized, and the static method already exists at the time the class is cr
. ”Set EntrySet (): Returns the view set of the Map.entry object, which is the keyword/value pair in the image"Because the mappings are unique, you use set support." You can also remove elements from the view, and these elements will be removed from the source image, but you cannot add any elements. ”Map.entry interfaceThe map's EntrySet () method returns a collection of objects that implement the Map.entry
of the first occurrence of the specified element if the element does not exist return-1;So, by 1, you can tell if an element exists.int lastIndexOf (Object o): Reverse index Specifies the position of the element.List sublist (start,end): Gets the child list.4, modify:Object Set (index,element): Modifies elements of the specified index bit.5, get all the elements:Listiterator Listiterator (): A list collection-specific iterator.The list collection supports adding, deleting, changing, and checkin
over the elements of the map passed, in addition to the algorithm that the iteration put () does to add each key-value pair to the map. It should be noted, however, that Putall () can correctly resize the map before adding all the elements, so if you do not adjust the size of the map yourself (which we will briefly describe), Putall () may be more efficient than expected.View MapThe elements in the iteration Map do not have a straightforward method. If you are querying a map to see which elemen
iterations cannot use the Get method and can be traversed using iterators and enhanced for loops. Map In class, the main HashMap, it is a set of key-value pairs, key keys are not allowed to repeat, a key can only correspond to one value, but a value can correspond to more than one key. Used to pass data between different pages. For example, the use of Request.setattribute ()/getattribute () in JSP is implemented using this kind of collection. HashMap there is a method
Putall () can correctly resize the map before adding all the elements, so if you do not adjust the size of the map yourself (which we will briefly describe), Putall () may be more efficient than expected.View MapThe elements in the iteration Map do not have a straightforward method. If you are querying a map to see which elements satisfy a particular query, or if you want to iterate over all of its elements, regardless of the cause, you first need to get the view of the map. There are three pos
Map interface
A map is an object that maps a key to a value. A map cannot contain duplicate keys: Each key can map a value at most. The map interface looks like this:
public interface Map { // Basic Operations Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk Operations void putAll(Map t); void clear(); // Collection Views public Set keySet(); pu
Generics (generic) reverses all elements in the arrayMy Java Foundation has been not very good, recently took the time to strengthen their basic knowledge, basic skills, to go more practical.For the map collection, we have to use the enhanced for loop mechanism skillfully (for each), where EntrySet method is important.@Test Public voidTest1 () {MapString,Integer> Map = NewHashMapString,Integer>();Map.Put"AA",1);Map.Put"BB",2);SetEntryString,Integer>>
the rest of the elements returned: The enumeration set that initially contains the specified element is thrown: nullpointerexception-If any argument is null, or rest is nullenumset(iterator2.2. Noneof methodnoneof (classenumset Class); Enumset2.add (Weather.sunny); Enumset2.add (Weather.rainy); For (iterator 3. List Save enumeration type:New Arraylist(iterator4, Enummap class:enummapThe private MAP implementation used with the Enum type key. All keys in an enumeration map must come from a singl
contains both the key and value parts, while a enry key does not provide a modification method, and value allows for modification. It is necessary to note that if a mutable object is used as the key of the map, the behavior of the map is indeterminate (JDK1.6 document) If the change Equals () is different from the previous behavior.For abstract class Abstractmap, most of the implemented methods are based on the abstract method returned by the set of all Entry
return all the keys of the collection, the other one returns a collection of all values, and then a return key and value combined into the EntrySet object collection, Map has a Get method, the parameter is key, The return value is a key corresponding to the value, this free play, nor the ability to test methods, these programming process will be prompted, combined with the differences between the three of them to say the use of the line.1 1. String s
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.