Java Collection--map Interface

Source: Internet
Author: User

1. Definition

Map is used to save the data that exists in the mapping relationship <key,value>. Where the key value cannot be duplicated (compared using the Equals () method), value values can be repeated

2. Methods

Put (Key,value): adding elements

Get (Object key): Gets the element of the specified key

KeySet (): Returns the collection of key for the specified object

VALUES (): Returns a collection of the specified object value

Remove (Object key): Removes the element of the specified key and returns a reference to the corresponding value

EntrySet (): Returns the view of the map that contains the mapping relationship

3. Common Implementation Classes

HashMap: Similar to HashSet, the Hashcode () method of key keys determines the storage location, unordered

TreeMap: Used to sort keys in the same way as TreeSet

Linkedhashmap: Similar to Linkedhashset

4. Example

Import static org.junit.assert.*;
Import java.util.Collection;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import Java.util.Set;
Import Org.junit.Test;
Import Day8. Customer;


public class TestMap {
@Test
public void Testmapmethods () {
Map m1=new HashMap ();
//put (key,value) add element
M1.put ("AA", New Customer (1, "AA"));
M1.put ("BB", New Customer (2, "BB"));
M1.put ("CC", New Customer (3, "CC"));
M1.put ("DD", New Customer (4, "DD"));
System.out.println (M1.size ());
Map m2=new HashMap ();
M2.put ("AA", New Customer (1, "AA"));
M2.put ("BB", New Customer (2, "BB"));
M2.put ("CC", New Customer (3, "CC"));
M2.put ("DD", New Customer (4, "DD"));
map<string,object> m3=new HashMap ();
M3.put ("AA", New Customer (1, "AA"));
M3.put ("BB", New Customer (2, "BB"));
M3.put ("CC", New Customer (3, "CC"));
M3.put ("DD", New Customer (4, "DD"));
when comparing two objects for equality, override the Equals () method of object, and return True if the key-value pair is exactly the same.
System.out.println (M1.equals (m2));
//get (Object key) Gets the element of the specified key
System.out.println (M1.get ("BB"));
//keyset () returns the collection of the specified object key
Set Keyset=m1.keyset ();
for (Object Key:keyset) {
SYSTEM.OUT.PRINTLN (key);
}
//values () returns the collection of the specified object value
Collection value=m1.values ();
for (Object Val:value) {
System.out.println (Val);
}
System.out.println ();
//Traversal operations
//Method One: Get the set of keys first, get the value when the key is traversed
for (Object Key:keyset) {
Object Val=m1.get (key);
System.out.println (key+ ":" +val);
}
System.out.println ();
//Method Two: Get a set of key-value pairs
For (entry<string,object> Entry:m3.entrySet ()) {
System.out.println (Entry.getkey () + ":" +entry.getvalue ());
        }
System.out.println ();
//remove (Object key) removes the element of the specified key and returns a reference to the corresponding value
Object obj=m1.remove ("AA");
System.out.println (obj);
System.out.println (M1.size ());
}
}

Full code involved: http://www.cnblogs.com/jfl-xx/p/4707643.html

Java Collection--map Interface

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.