Analysis of map in Java

Source: Internet
Author: User

Map: store key-value pairs and ensure the uniqueness of keys.
 | -- Hashmap: uses the data structure of the hash table to determine that duplicate elements must overwrite the hashcode and equals methods. hashset is obtained by hashmap.
    The thread is not secure. It can store null keys and null values.
 | -- Hashtable: The data structure is also a hash table. It is thread-safe and cannot store null keys and null values. It is inefficient and replaced by hashmap.
 | -- Treemap: uses a binary tree data structure. You can sort keys in the treemap set.
 
 ※Note: 1. differentiate hashmap and hashtable: the thread security is different. Can null values be stored for efficiency.
   2,

Principle of retrieving elements from a map set:
 First, the map is converted into a set, and then the set is iterated.
 Keyset: Place all keys in the Set set, and retrieve the value based on the key value obtained from the set.
 Entryset: extracts the key-value relationship and saves it to the Set set. The supervised relationship has its own type, which is set in the map interface.
   You can use the getkey () and getvalue () methods to obtain values,

When can I use a map set?
---- When there is a ing relationship between objects, you need to use the map set.

Import java. util .*;
Class treemapdemo
{
 Public static void main (string [] ARGs)
 {
  Treemap <student, string> Hm = new treemap <student, string> (
  New comparator <student> ()
  {
   Public int compare (student ST1, student st2)
   {
    If (st1.getage ()> st2.getage ())
     Return 1;
    Else if (st1.getage () = st2.getage ())
     Return st1.getname (). compareto (st2.getname ());
    Return-1;
   }
  });
  
  Hm. Put (new student ("James", 20), "Beijing ");
  Hm. Put (new student ("Li Si", 21), "Xi'an ");
  Hm. Put (new student ("Wang Wu", 15), "Chongqing ");
  Hm. Put (new student ("Zhao liu", 26)," Chengdu ");
  Hm. Put (new student ("Sun Yue", 25), null ); // Hashmap can store null values, but hashtable cannot. Therefore, the get method cannot be used to return NULL to determine whether the key value exists. Instead, the containskey method should be used to determine whether the key value exists.
  // Note: The put method has a return value, which returns the value of the key-value correspondence relationship. The original key-value relationship in the set is not specified. If yes, the original value is returned.
  String A = hm. Put (new student ("Xiao Qi", 12), "Mianyang "); // No key-value (new student ("Xiao Qi", 12) is specified in the original set. The return value is null.
  // Hm. Put (new student ("Xiao Qi", 12), "Mianyang"); // when the hashcode method and equals method are not covered, the same elements can be saved.
  String B = HM. put (new student ("Xiao Qi", 12), "Taiyuan"); // The original ing relationship for this key value (new student ("Xiao Qi", 12, returns the specified value. here, Taiyuan covers the original Mianyang.
  System. Out. println (a + "..." + B );

  Set <map. Entry <student, string> S = hm. entryset ();
  For (iterator <map. Entry <student, string> it = S. iterator (); it. hasnext ();)
  {
   Map. Entry <student, string> me = it. Next ();
   Student Stu = me. getkey ();
   String ADDR = me. getvalue ();
   System. Out. println (STU. getname () + "..." + Stu. getage () + "..." + ADDR );
  } 
 }
}

Class student
{
 Private string name;
 Private int age;
 Student (string name, int age)
 {
  This. Name = Name;
  This. Age= Age;
 }

 Public String getname ()
 {
  Return name;
 }
 Public int getage ()
 {
  Return age;
 }

 Public int hashcode ()// Note that the uniqueness of the hashset and hashmap sets is determined, covering the hashcode and equals methods, while the treemap and treemap sorting implement the comparable or comparator interfaces.
 {
  Return name. hashcode () + age * 29;
 }

 Public Boolean equals (Object OBJ)
 {
  If (! (OBJ instanceof student ))
   Return false;
  Student = (student) OBJ;
  Return this. Name. Equals (student. Name) & this. Age = student. Age; 
 }

 Public String tostring ()
 {
  Return name + "..." + 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.