Map notes summary, map notes

Source: Internet
Author: User

Map notes summary, map notes

Map: stores key-value pairs. A pair of key-value pairs must be unique.

The common three subclasses of Map.
1. The bottom layer of HashTable is a hash table structure and cannot be null. The set is thread-synchronized. The efficiency is low.
This class implements a hash table that maps keys to corresponding values. Any non-null object can be used as a key or value.

2. The bottom layer of HashMap is the data structure of the hash table. The null value and the null key are allowed. This set is not synchronized. The efficiency is high.

3. The underlying layer of TreeMap is a binary tree structure. The thread is not synchronized and has the ordering feature. You can sort keys in the Map set.
Similar to Set, the underlying layer of Set sets uses the Map Set method.

Map has the following common methods:

1. Add
Put ()
PutAll ()
2. Delete
Clear ();
Remove ();
3. Judgment
ContainsValue (Object value)
ContainsKey (Object key)
IsEmpty ();
4. Get
Get ();
Size ();
Values ();

Code demonstration of basic common methods:

Import java. util. *; class MapDemo {public static void main (String [] args) {Map <String, String> map = new HashMap <String, String> (); // Hash unordered. // Add an element with the same key. The added value will replace the value corresponding to the original key. the put method returns the previously overwritten value. sop ("Put:" + map. put ("01", "zhangsan01"); sop ("Put:" + map. put ("01", "wangwu"); map. put ("02", "zhangsan02"); map. put ("03", "zhangsan03"); // determines whether sop ("containsKey: zhangsan01" + "------" + map. containsKey ("01"); // sop ("remove:" + map. remove ("03"); // search for the key and print the corresponding value: zhangsan03 // obtain sop ("get:" + map. get ("02"); map. put ("04", null); sop ("get:" + map. get ("04"); // you can use the return values of a get method to determine whether a key exists. returns null. // obtain all values in the Map set Collection <String> coll = map. values (); sop (coll); sop (map);} public static void sop (Object obj) {System. out. println (obj );}}

 

Key Map methods:
EntrySet ()
KeySet ()
These two methods are used to retrieve elements from the Map set:

The Map set can be retrieved in either of the following ways:


1. Set <k> keySet
Store all the keys in the Map to the Set. Set has the iterator, and all the keys can be retrieved by iteration.
Obtain the value of each key based on the get method.

Principle of Map Collection removal: Convert to Set.

 

2. Set <Map. Entry <k, v> entrySet

Store the ing relationships in the map Set to the Set. The data type of the ing is Map. Entry.


The Entry is actually an internal interface. It is defined in the map interface.

The format is as follows:
Interface Map
{
Public static interface Entry
{
Public static void getValue ();
Public static void getKey ();
}

}

Class HashMap implements Map
{
Class Hash implements Map. Entry
{
Public Object getValue (){};
Public Object getKey (){};
}
}


Code drills for two removal methods:

/* Two methods to retrieve the Map Set: 1.Set< k> keySet stores all the keys in the map to the Set. set has an iterator, and all keys can be retrieved by iteration. obtain the value of each key lock Based on the get method. principle of Map Collection removal: Convert to Set. in the iterator. 2.Set< Map. entry <k, v> entrySet: stores the ing relationships in the map Set to the Set. The data type of the ing is Map. the Entry is actually an internal interface. define interface map {public static interface Entry {public static void getValue (); public static void getKey () ;}} class HashMap implements Map {class Hash implements Map in the Map interface. entry {public Object getValue () {}; public Object getKey () {}}} */import java. util. *; class MapDemo2 {public static void main (String [] args) {Map <String, String> map = new HashMap <String, String> (); map. put ("01", "zhangsan01"); map. put ("02", "zhangsan02"); map. put ("03", "zhangsan03"); map. put ("04", "zhangsan04"); // extracts the ing from the map set. set <Map. entry <String, String> mapEntry = map. entrySet (); Iterator <Map. entry <String, String> it = mapEntry. iterator (); while (it. hasNext () {Map. entry <String, String> me = it. next (); String key = me. getKey (); String value = me. getValue (); sop ("Key:" + key + "Value:" + value);}/* // obtain all the keys of the Map Set and place them in the Set. how to obtain: keySet (); Set <String> keySet = map. keySet (); // Retrieves all keys by iteration. iterator <String> it = keySet. iterator (); while (it. hasNext () {String key = it. next (); // with the key, you can use the get (key) method of the Map set to obtain the corresponding value. string value = map. get (key); sop ("Key:" + key + "Value:" + value);} */} public static void sop (Object obj) {System. out. println (obj );}}

 

Related exercises:

/* Exercise: Each student has a corresponding region. Key-Value Pair: students are the key, and the address type is String. Student attributes: name, age. Note: If the names and ages are the same student. Ensure the uniqueness of students. 1. Description of the student. 2. Define the map container. Take the student as the key and the address as the value. Save. 3. Obtain the elements in the map set. */Import java. util. *; class MapTest {public static void main (String [] args) {Map <Student, String> map = new HashMap <Student, String> (); map. put (new Student ("zhangsan01", 21), "beijing"); map. put (new Student ("zhangsan02", 22), "shanghai"); map. put (new Student ("zhangsan03", 23), "guangzhou"); map. put (new Student ("zhangsan04", 24), "shenzhen"); // Method 1: keySet Set <Student> keySet = map. keySet (); Iterator <Stud Ent> it = keySet. iterator (); while (it. hasNext () {Student stu = it. next (); String addr = map. get (stu); sop ("First fetch method: student:" + stu. getName () + ", age:" + stu. getAge () + ", address:" + addr);} sop ("-----------------------------------------"); // Method 2: entrySet <Map. entry <Student, String> entrySet = map. entrySet (); Iterator <Map. entry <Student, String> iter = entrySet. iterator (); while (iter. hasNe Xt () {Map. entry <Student, String> me = iter. next (); Student stu = me. getKey (); String addr = me. getValue (); sop ("Method 2: student:" + stu. getName () + ", age:" + stu. getAge () + ", adresss:" + addr) ;}} public static void sop (Object obj) {System. out. println (obj) ;}} class Student implements Comparable <Student> {private String name; private int age; Student (String name, int age) {this. name = name; this. Age = age;} public int compareTo (Student s) {if (! (S instanceof Student) throw new ClassCastException ("type matching failed"); int num = new Integer (this. age ). compareTo (new Integer (s. age); if (num = 0) {return this. name. compareTo (s. name);} return num;} public int hashCode () {return name. hashCode () + age * 17;} public boolean equals (Object obj) {if (! (Obj instanceof Student) throw new ClassCastException ("type matching failed"); Student stu = (Student) obj; return this. getName (). equals (stu. getName () & this. age = stu. age;} public void setName (String name) {this. name = name;} public void setAge (int age) {this. age = age;} public String getName () {return name;} public int getAge () {return age ;}}

 

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.