Java Learning Lesson 38th (Common Object API)-collection framework (vi)-MAP collection and its common methods

Source: Internet
Author: User

The characteristics of map collection

Map collection (double column collection) add a pair of elements at a time, collection collection (single row collection) add one element at a time

The Map<k,v> interface is actually stored as a key-value pair. Features:

The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value.

In other words, the uniqueness of the key must be guaranteed in the map

Second, common methods

1. Add: Value = put (Key,value); Returns the value associated with the previous key, if no null2 is returned. Delete: void clear (); Clears all mappings in map value remove (key): Based on the specified key, Delete the Key-value key value pair 3. Judgment: Boolean ContainsKey (Key), Boolean containsvalue (Value), Boolean IsEmpty (), 4. Get: Value Get (key): The key gets the value, and if it does not, returns NULL, which means you can tell that the map does not have an int size (): Gets the number of key-value pairs

Method Demo

Import Java.util.hashmap;import Java.util.map;public class Main {public static void main (string[] args) {Map<integer, string> map = new hashmap<integer,string> (); method (map);} public static void Method (Map<integer, string> Map) {//Name number//1. Add System.out.println (Map.put (1, "a")); System.out.println (Map.put (1, "B"));//Save the same key, the value will be overwritten Map.put (3, "C"); Map.put (4, "D"); SYSTEM.OUT.PRINTLN (map);//delete Map.Remove (3); SYSTEM.OUT.PRINTLN (map);//Judge System.out.println ("ContainsKey:" +map.containskey (3)); System.out.println ("Containsvalue:" +map.containsvalue ("D"));//Get System.out.println ("Get:" +map.get (4)); System.out.println ("Get:" +map.get (8));//judgment 8 in Not}}

map and collection are not related, so there is no iterator, so how to get all the key-value pairs in the map (two methods)

1. Through the Ketset () method (key Mastery), return a view of the keys contained in this map Set .

Import Java.util.hashmap;import java.util.iterator;import Java.util.map;import Java.util.set;public class Main { public static void Main (string[] args) {map<integer, string> Map = new hashmap<integer,string> (); Keysetmehod (map);} public static void Keysetmehod (Map<integer, string> Map) {map.put (1, "a"), Map.put (4, "B"), Map.put (3, "C"); Map.put (5, "D");//Remove All key-value pairs in the map and take three steps to//1. By keyset (), the set of all keys is located, and the set has an iterator//2. Through iterators, get the KEY//3. Get the corresponding valueset<integer> keyintegers = Map.keyset ();iterator<integer> it = by getting () in the map Keyintegers.iterator (); while (It.hasnext ()) {Integer key = It.next (); System.out.println ("Key-value:" +key+ "-" +map.get (key));}}}

Three steps.


2. Pass entrySet() : Returns a view of the mappings contained in this map Set .

import Java.util.hashmap;import java.util.iterator;import Java.util.map;import Java.util.set;public class Main {public static void Main (string[] args) {map<integer, string> Map = new hashmap<integer,string> (); Keysetmehod (map);} public static void Keysetmehod (Map<integer, string> Map) {map.put (1, "a"), Map.put (4, "B"), Map.put (3, "C"); Map.put (5, "D");//The type of the mapping relationship: Map.entry type//map.entry<k, v> method/*boolean equals (object o) compares the equality of the specified object to this item. K GetKey () returns the key corresponding to this key. V GetValue () returns the value corresponding to this key. int Hashcode () returns the hash code value for this mapping entry.  V SetValue (v value) replaces the value corresponding to this item with the specified value (optional operation). */set<map.entry<integer,string>> Entry = Map.entryset ();iterator<map.entry<integer,string> > it = Entry.iterator (); while (It.hasnext ()) {//it.next is the mapping relationship map.entry<integer,string> Maentry = It.next (); System.out.println ("Key-value:" +maentry.getkey () + "-" +maentry.getvalue ());}}} 


About Map.entry<key,value> Strengthening understanding

Interface Mmap{public static interface entry{void get ();}} Class XX implements mmap.entry{...}

It's just a nesting.

3. Through the values () method: Returns a view of the values contained in this map Collection .

Import Java.util.collection;import java.util.hashmap;import Java.util.iterator;import Java.util.Map;import Java.util.set;public class Main {public static void Main (string[] args) {map<integer, string> Map = new Hashmap<i Nteger,string> (); Keysetmehod (map);} public static void Keysetmehod (Map<integer, string> Map) {map.put (1, "a"), Map.put (4, "a"), Map.put (3, "C"); Map.put (5, "D"); Collection<string> cel = map.values ();iterator<string> it = Cel.iterator (); while (It.hasnext ()) {String in = It.next (); System.out.println ("Key-value:" +in);}}

Java Learning Lesson 38th (Common Object API)-collection framework (vi)-MAP collection and its common methods

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.