Java Collection Map interface and Map.entry Learning

Source: Internet
Author: User

The map interface is not an inheritance of the collection interface. The map interface is used to maintain key/value pairs (Key/value pairs). This interface describes a key-to-value mapping that is never duplicated.

(1) Add, delete operation:
Object put (object key, Object value): Places a keyword associated with one value into the image. If the keyword already exists, the new value associated with this keyword supersedes the old value. method returns the old value of the keyword, or null if the keyword does not originally exist
Object remove (Object key): Remove key-related mappings from the image
void Putall (Map t): Adds all elements from a specific image to the image
void Clear (): Remove all mappings from the image
Both the key and the value can be null. However, you cannot add a map as a key or value to itself. ”
(2) Query operation:
Object get (Object key): Gets the value associated with the keyword key and returns the object associated with the keyword key, or null if no keyword is found in the image
Boolean ContainsKey (Object key): Determine if there is a keyword key in the image
Boolean Containsvalue (Object value): Determines whether the value exists in the image
int size (): Returns the number of mappings in the current image
Boolean IsEmpty (): Determine if there are any mappings in the image
(3) View actions: Working with the key/value pairs group in the image
Set KeySet (): Returns the set of views for all keywords in the image
"Because the collection of keys in the map must be unique, you use set support. You can also delete an element from the view, while the keyword and its associated values are removed from the source image, but you cannot add any elements. ”
Collection values (): A view set that returns all values in the image
"Because the collection of values in the map is not unique, you use collection support. You can also delete an element from the view, while the value and its keywords will be removed from the source image, but you cannot add any elements. ”
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 interface
The map's EntrySet () method returns a collection of objects that implement the Map.entry interface. Each object in the collection is a specific key/value pair in the underlying map.

With this set of iterators, you can get keys or values for each entry (the only way to get it) and change the value. When an entry is returned through an iterator, unless it is the SetValue () method of the iterator's own remove () method or the entry returned by the iterator, the remainder of the modification to the source map will cause this set of entries to become invalid and the resulting entry behavior undefined.
(1) Object GetKey (): Returns the keyword of the entry
(2) Object getValue (): Returns the value of an entry
(3) Object SetValue (object value): Changes the value in the associated image to value and returns the old value

Example: Java code

Map<string,integer> map=new hashmap<string,integer> ();

Map.put ("1", 1);

Map.put ("2", 2);

Map.put ("3", 3);

Map.put ("3", 3);

Iterator Itor=map.entryset (). Iterator ();

while (Itor.hasnext ()) {

Map.entry<string,integer> entry= (map.entry<string,integer>) itor.next ();

System.out.println ("key=" +entry.getkey (). toString ());

System.out.println ("values=" +entry.getvalue (). toString ());

}

Java Collection Map interface and Map.entry Learning

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.