Java learning note _ 24_Map Interface

Source: Internet
Author: User

24. Map interface:

The Map interface defines the method for storing "key-value ing pairs". The Map cannot contain duplicate "keys ",

The "key-value" ing pairs stored in the Map implementation class are uniquely identified by keys. The "key" at the underlying Map layer is stored using Set,

Therefore, the classes corresponding to the ing "key" in the Map must overwrite the hashCode () and equals () methods,

String is commonly used as the "key" of Map ".

Map addition and deletion operations:

· Object put (Object key, Object value): stores the correlated "key-value" in the image. If this keyword already exists,

Then the new value related to this keyword will replace the value; otherwise, null is returned.

· Object remove (Object key): removes the "key-value" from the Map based on the specified key.

· Void putAll (Map t): Adds all elements from a specific image to the image.

· Void clear (): deletes all images from the image.

Query operations on elements in Map:

· Object get (Object key): obtains key-related values and returns the objects related to the key,

If not found, null is returned.

· Boolean containsKey (Object key): determines whether a keyword key exists in the image.

· Boolean containsValue (Object value): determines whether the image has a value.

· Int size (): returns the number of current images.

· Boolean isEmpty (): determines whether the image is empty.

Element View method in Map:

· Set keySet (): return the view set Set with all the keywords in the image. You can also delete the "key-value" pair from the view,

However, you cannot add a key-value pair.

· Collectio values (): returns all value-based datasets in the image. You can also delete "key-value" pairs from the view,

However, you cannot add a key-value pair.

· Set entrySet (): return the "key-value" pair in the image. You can also delete the "key-value" pair from the view,

However, you cannot add a key-value pair.

1> Implementation class HashMap:

HashMap is a container with the highest frequency. It stores "keys" in three columns using Set. Therefore, it is highly efficient to retrieve "values" based on "keys,

In addition, it allows the use of null values and null keys, but does not guarantee the ing order.

The following is an example of HashMap usage:

Import java. util. hashMap; import java. util. iterator; import java. util. set; public class HashMapTest {public static void main (String [] args) {// declare a HashMap object HashMap
 
  
Hmp = new HashMap
  
   
(); // Add the "key-value" pair to hmp. put ("1", "d"); hmp. put ("0", "c"); hmp. put ("3", "a"); hmp. put ("2", "B"); hmp. put ("5", "e"); hmp. put ("4", "g"); // output "key-value" to System. out. println ("HashMap: \ n
   
    
"); System. out. println ("\ n displays" key-value "pairs:"); System. out. println (hmp); // Delete the "key-value" pair hmp. remove ("3"); hmp. remove ("2"); System. out. println ("\ n displays the" key-value "pair after" key-value "is deleted:"); System. out. println (hmp); // obtain valueSystem from the key. out. println ("\ n gets the corresponding value from the key:"); System. out. println ("4 =" + hmp. get ("4"); // obtain all key values. The return type is SetSet.
    
     
Set = hmp. keySet (); // determines whether the keyword keySystem exists. out. println ("\ n determines whether a specified key exists:"); System. out. println ("key7:" + hmp. containsKey ("7"); // determines whether a specified valueSystem exists. out. println ("\ n determines whether a specified value exists:"); System. out. println ("whether key4 exists" + hmp. containsValue ("g"); // determines whether the container is empty. out. println ("\ n container is empty:" + hmp. isEmpty (); // output the container capacity System. out. println ("\ n output container capacity:" + hmp. size (); // generate an Iterator
     
      
It = set. iterator (); // use the iterator generated by the key to output the value System for all "key-value" pairs. out. println ("\ n output value:"); while (it. hasNext () {System. out. print (hmp. get (it. next () + ";");}}}
     
    
   
  
 

Output result:

HashMap:

Show "key-value" pairs:

{3 = a, 2 = B, 1 = d, 0 = c, 5 = e, 4 = g}

The "key-value" pairs after "key-value" are deleted:

{1 = d, 0 = c, 5 = e, 4 = g}

Get the corresponding value from the key:

4 = g

Determine whether a specified key exists:

Key7: false

Determine whether a specified value exists:

Key4: true

Empty Container: false

Output container capacity: 4

Output value:

D; c; e; g;

We can see that the output sequence of the "key-value" pair is different from that of the output sequence.

2> Implementation class LinkedHashMap:

The LilkedHashMap class is a sub-class of hashMap. It can sort the elements according to the inserted sequence, which is highly efficient for adding, deleting, and modifying elements.


The following is an example of LinkedHashMap:

Import java. util. iterator; import java. util. linkedHashMap; import java. util. set; import java. util. map; public class HashMapTest {public static void main (String [] args) {// declare a LinkedHashMap object Map
 
  
Lhmp = newjavashashmap
  
   
(); // Add the key-Value Pair lhmp to lhmp. put (1, "a"); lhmp. put (0, "c"); lhmp. put (3, "a"); lhmp. put (2, "B"); // output "key-value" to System. out. println ("LinkedHashMap: \ n
   
    
"); System. out. println (lhmp); // obtain all key values. Its return type is SetSet.
    
     
Set = lhmp. keySet (); // generate an Iterator
     
      
It = set. iterator (); // use the iterator generated by the key to output the System. out. println ("
      
        "); While (it. hasNext () {System. out. print (lhmp. get (it. next () + ";");}}}
      
     
    
   
  
 

Output result:

{1 = a, 0 = c, 3 = a, 2 = B}

A; c; a; B;

We can see that the input sequence of "key-value" exactly corresponds to the output sequence. If the key is re-inserted in the ing, the insertion sequence is not affected.

3> Implementation class TreeMap:

The "key" of the "key-value" pairs stored in the TreeMap must be sortable.

The following example demonstrates the usage of TreeMap:

Import java. util. iterator; import java. util. map; import java. util. set; import java. util. treeMap; public class TreeMapTest {public static void main (String [] args) {// declare a TreeMap object Map
 
  
Tmp = new TreeMap
  
   
(); // Add the key-Value Pair lhmp to lhmp. put (1, "a"); lhmp. put (0, "c"); lhmp. put (3, "a"); lhmp. put (2, "B"); // output "key-value" to System. out. println ("TreeMap: \ n
   
    
"); System. out. println (tmp); System. out. println ("output container size:" + tmp. size (); // obtain all key values. Its return type is SetSet.
    
     
Set = tmp. keySet (); // generate an Iterator
     
      
It = set. iterator (); // use the iterator generated by the key to output the System. out. println ("
      
        "); While (it. hasNext () {System. out. print (tmp. get (it. next () + ";");}}}
      
     
    
   
  
 

Output result:

LinkedHashMap:

{0 = c, 1 = a, 2 = B, 3 =}

Output container size: 4

C; a; B;;

In TreeMap, keys must be sorted. In internal storage, keys are stored in sequence and the output is the same.

4> Implementation class Properties:

The Properties class represents a persistent property set, which can be saved in a stream or loaded from a stream. The stored "key-value" pairs are strings,

The setProperties (String key, String value) method and getProperty (String key) method should be used for access.

The following is an example of the Properties class:

Import java. io. IOException; import java. io. inputStream; import java. util. properties; public class TreeMapTest {public static void main (String [] args) {// get the file and read it into the input stream InputStream is = (InputStream) Thread. currentThread (). getContextClassLoader (). getResourceAsStream ("config. properties "); // create the property set object Properties prop = new Properties (); try {// load data prop from the stream. load (is);} catch (IOException e) {e. printStackTrace ();} // obtain the name attribute String name = prop. getProperty ("name"); System. out. println ("name =" + name); // obtain the password attribute String pwd = prop. getProperty ("password"); System. out. println ("password =" + pwd );}}

Output result:

Name = spilit

Password = abc123

Create the Fiel document config. properties in src:

# Key = value

Name = spilit

Password = abc123






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.