Java 15th _ map set,

Source: Internet
Author: User
Tags set set

Map set:

This set stores key-value pairs. One-to-one storage. The key must be unique.

When there is a ing relationship between elements, consider creating a map set.

Method Abstract:

 

1. Add: if the same key is displayed when the key is added. Then the added value overwrites the corresponding value of the original key, and the put method returns the overwritten value.

Put (K key, V value)

Putall (Map <? Extends K ,? Extends v> m)

2. Delete

Clear ()

Remove (Object key)

3. Judgment

Containvalue (object value)

Containskey (Object key)

Isempty ()

4. Get

Get (Object key): You can use the return value of the get method to determine whether a key exists. Returns null.

Size ()

Values ()

 

Entryset ()

Keyset ()

Map System: similar to set. At the underlying layer of set, map sets are used.

Map

| -- Hashtable: The underlying layer is the data structure of the hash table, and the null key value cannot be saved. This set is synchronized by threads. Jdk1.0. Low efficiency.

| -- Hashmap: The underlying layer is the data structure of the hash table. A null key value is allowed. This set is not synchronized. Jdk1.2. High efficiency.

| -- Treemap: The underlying layer is the binary tree data structure and threads are not synchronized. It can be used to sort keys in the map set.

 

There are two methods to retrieve a map set:

Principle of Map Collection removal: converts a map set into a set. Then, it is retrieved through the iterator.

1. Set <k> keyset: stores all the keys in the map to the Set set. Because set has an iterator, all keys can be retrieved iteratively, and the corresponding values of each key can be obtained according to the get method.

Illustration:


2. Set <map. Entry <K, V> entryset: saves the ing relationships in the map set to the Set set. The data type of the ing is map. Entry.

The entryset () method extracts the ing relationships from the map set. This relationship is of the map. Entry type. After the relational object map. Entry is obtained, the key and value in the link can be obtained through the getkey and getvalue methods in map. Entry.

Illustration:


Code:

 

// Import Java. util. collection; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import Java. util. set; public class mapdemo {/*** @ Map Method demonstration. */Public static void main (string [] ARGs) {Map <string, string> map = new hashmap <string, string> (); // map is not a specific collection class. // Add an element. Map. put ("01", "zhangsan1"); map. put ("02", "zhangsan02"); map. put ("03", "zhangsan03"); // system. out. println (map. put ("03", "zhangsan033"); // The value added after map overwrites the previous value and returns the overwritten value. Map. Put ("04", null); // delete an element. // System. out. println (map. remove ("02"); // map. clear (); // clear () No return value // get element // system. out. println (map. get ("04"); // obtain the value based on the specified key. If this key-value pair is not available, null is returned. // Collection <string> coll = map. Values (); // obtain the value in map. Save these values to the collection. // Retrieve the element // The first method to save the map key value to the Set set. Iterator out the key value, and then extract the value based on the key value. /* Set <string> keyset = map. keyset (); For (iterator <string> it = keyset. iterator (); it. hasnext ();) // {string key = it. next (); string value = map. get (key); system. out. println ("Key =" + key + ", value =" + value);} * // method 2. Extracts the ing relationship of the map. To the Set set. Set <map. entry <string, string> entry = map. entryset (); For (iterator <map. entry <string, string> it = entry. iterator (); it. hasnext ();) {map. entry <string, string> me = it. next (); string key = me. getkey (); string value = me. getvalue (); system. out. println (Key + ":" + value);} // system. out. println (Coll );}}

 

Package day16; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import Java. util. set; public class studentmap {/*** @ map the student object is the key and the corresponding address is the value. */Public static void main (string [] ARGs) {hashmap <student, string> Hm = new hashmap <student, string> (); HM. put (new student ("zhangsan01", 20), "Beijing"); HM. put (new student ("zhangsan01", 20), "Beijing"); HM. put (new student ("zhangsan02", 22), "Shanghai"); HM. put (new student ("zhangsan03", 23), "Tianjin"); set <map. entry <student, string> HS = HM. entryset (); For (iterator <map. entry <student, string> it = HS. iterator (); it. Hasnext ();) {map. entry <student, string> me = it. next (); Student Stu = me. getkey (); string add = me. getvalue (); system. out. println (STU + "^" + Add) ;}} class student implements comparable <student> {private string name; private int age; Public String getname () {return name ;} public void setname (string name) {This. name = Name;} public int getage () {return age;} public void setage (INT age) {This. age = age;} student (string Name, int age) {This. Name = Name; this. Age = age;} public int compareto (student s) // sort. {Int num = new INTEGER (this. age ). compareto (New INTEGER (this. age); If (num = 0) return this. name. compareto (S. name); Return num;} public int hashcode () // rewrite the hashcode method. Try to make the return values of each element different. {Return name. hashcode () + age * 10;} public Boolean equals (student s) // rewrite the equals method. {Return this. Name. Equals (S. Name) & (this. Age = S. Age);} Public String tostring () // This method must be called to print objects. {Return name + ":" + age ;}}

 

Import Java. util. iterator; import Java. util. map; import Java. util. set; import Java. util. treemap; public class charcount {/*** @ count the number of times letters appear in the string and store them to the Set */public static void main (string [] ARGs) {string STR = "aldjflasdfoqwnieonvaospnfieo"; system. out. println (charcount (STR);} public static string charcount (string Str) {char [] CHS = Str. tochararray (); treemap <character, integer> TM = new treemap <character, integer> (); int Count = 0; For (INT x = 0; x <CHS. length; X ++) {If (! (CHS [x]> = 'A' & CHS [x] <= 'Z' | CHS [x]> = 'A' & CHS [x]> = 'Z ')) continue; integer value = TM. get (CHS [x]); If (! (Value = NULL) {COUNT = value;} count ++; TM. put (CHS [X], count);/* If (value = NULL) {TM. put (CHS [X], 1) ;}else {value ++; TM. put (CHS [X], value);} */COUNT = 0;} stringbuffer sb = new stringbuffer (); set <map. entry <character, integer> entryset = TM. entryset (); For (iterator <map. entry <character, integer> it = entryset. iterator (); it. hasnext ();) {map. entry <character, integer> me = it. next (); character CH = me. getkey (); integer in = me. getvalue (); sb. append (CH + "(" + in + ")");} return sb. tostring ();}}

 

 

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.