Map interface learning and Map interface Learning

Source: Internet
Author: User

Map interface learning and Map interface Learning

Interface Map <K, V>

 

I. Map Function

1. Add

Put (K key, V value)

PutAll (Map <? Extends K ,? Extends V> );

 

2. Delete

Clear ()

Remove (Object key); returns the corresponding value

 

3. Judgment

ContainsKey (Object key)

ContainsValue (Object value)

IsEmpty ()

 

4. Get

Get (Object key): null is not returned

Size ()

Values ();

-------------------------------------

Key points: entrySet () and keySet ();

Two methods to retrieve a map set

1. keySet: Set <K>: return the set view of the key contained in the ing.

 

Because Set has an iterator, all keys can be retrieved through iteration and the get method can be used to obtain the values corresponding to each key.

Map <String, String> map = new HashMap <String, String> ();

Map. put ();

 

// Obtain the set of keys first

Set <String> ks = map. keySet ();

// Iterator

Iterator <String> it = ks. iterator ();

While (it. hasNext ()){

String key = it. next ();

String value = map. get (key );

System. out. println (key + "-->" + value );

}

 

2. entrySet: Set <Map. Entry <K, V>

ReturnsIngSet View

Map. Entry: saves the ing relationships in the map Set to the Set, and the Data Type of the ing is Map. Enty.

// Retrieve the ing relationships in the Map Set and store them to the Set.

Set <Map. Entry <String, String> entrySet = map. entrySet ();

Iterator <Map. Entry <String, String> it = entrySet. iterator ();

While (it. hasNext ()){

 

Map.Entry<String, String> me = it.next();

String key = me.getKey();

String value = me.getValue();

System.out.println(key + “  ” + value);

}

 

--------------------------------------

An Entry is also an interface, which is an internal interface of the Map interface.

Interface Map

{

 

public static Interface Entry{

         public abstract Object getKey();

         public abstract Object getValue();

}

}

Class HashMap implements Map

{

 

class HaHa implements Map.Entry{

         public Object getKey(){}

         public Object getValue(){}

}

 

 

}

-------------------------------------

2. Common younger siblings

 

Hashtable: The underlying layer is the data structure of the hash table. It cannot be saved to null. It is thread-synchronized (JDK1.0) with low efficiency.

HashMap: a Map interface based on a hash table. It can use null, which is not synchronized by threads. (JDK1.2) High Efficiency

TreeMap: based on binary trees, which can be used for key sorting instead of thread synchronization.

Set: the underlying layer of the set uses the Map Set.

 

Iii. Map Demo

 

Map<String, String> map = new HashMap<String, String>();

 

Map. put ("1", "cc ");

Sysout (map. containsKey (1 ));

Sysout (map. remove ("1 "));

Map. get ("1"); // null is not returned

Note:: Map. put ("abc", null) during HashMap );

 

This is acceptable. This is not allowed when HashTable is used.

// Values ()

Obtain all values in the map set.

Map. values (); // return Collection <V>

Collection <String> coll = map. values ();


Map Interface Usage Program

This is the generic Usage After jdk1.5.

Is traversing this map. Map <string, Integer> indicates that the key in the map is String type, and the value is Integer type.
For (String a: args)
This is to traverse out-of-the-box String arrays out of the public static void main (String args []), equivalent
For (int I = 0; I <args. length; I ++ ){
String a = args [I];
}
Integer freq = m. get (a); // This is to retrieve the Value in the Map by traversing every Value in the String []. It should be of the Integer type, corresponding to the previous Map <string, Integer>

// Output the length of the map m, which is the same as list. size. The number of key-value pairs in the map.
System. out. println (m. size () + "disinct words :")

What are the methods for Map interfaces?

Void clear ()
Remove all mappings from this ing (optional ).
Boolean containsKey (Object key)
Returns true if the ing contains the ing relationship of the specified key.
Boolean containsValue (Object value)
Returns true if this ing maps one or more keys to a specified value.
Set <Map. Entry <K, V> entrySet ()
Returns the set view of the ing relationships contained in the ing.
Boolean equals (Object o)
Compare whether the specified object is equal to this ing.
V get (Object key)
Returns the value mapped to the specified key in the ing.
Int hashCode ()
Returns the hash value of this ing.
Boolean isEmpty ()
Returns true if the ing does not contain a key-value ing relationship.
Set <K> keySet ()
Returns the set view of the key contained in the ing.
V put (K key, V value)
Associate the specified value with the specified key in the ing (optional ).
Void putAll (Map <? Extends K ,? Extends V> t)
Copies all mappings from a specified ing to this ing (optional ).
V remove (Object key)
If the ing relationship of this key exists, remove it from the ing (optional ).
Int size ()
Returns the number of key-value ing relationships in the ing.
Collection <V> values ()
Returns the collection view of the values contained in this ing.

Open the API to view it.

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.