Java set map interface Learning

Source: Internet
Author: User

The map interface is used to store element pairs (keys and values)

Methods In the map interface
Equals (Object O)
Hashcode
Clear ()
Remove (Object key)
Put (Object key, object value)
Putall (map t)
Entryset returns the set view containing the ing in the map. Each element in the set is a map. Entry object. You can use the getkey () and getvalue () methods (and the setvalue () method) to access the key elements and value elements of the latter.
Keyset () returns the set view of keys contained in the map. Deleting the elements in the set will also delete the corresponding ing (key and value) in the map)
Values () returns the collection view of values contained in the map. Deleting the elements in the collection will also delete the corresponding ing (key and value) in the map)

Get (Object key) returns the value associated with the specified key
Containskey (Object key) true or false;
Containsvalue (object Value) true or false;
Isempty ()
Size ()
1. hashmap
Hashmap provides all optional ing operations and allows the use of null values and null keys. Apart from non-synchronization and the use of null, The hashmap class is roughly the same as hashtable.

Note that this implementation is not synchronous. If multiple threads access a hash ing at the same time, and at least one thread modifies the ing from the structure, it must maintain external synchronization. This

By synchronizing the objects that encapsulate the ing. If such an object does not exist, use the collections. synchronizedmap method to "Wrap" The ing. It is best to complete this operation at creation to prevent unexpected non-synchronous access to the ing, as shown below:
Map M = collections. synchronizedmap (New hashmap (...));

A: traverse hashtable and use the entryset interface.

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://www.javaeye.com/topic/421014# ">
  1. Map <string, integer> map =NewHashmap <string, integer> ();
  2. Map. Put ("1", 1 );
  3. Map. Put ("2", 2 );
  4. Map. Put ("3", 3 );
  5. Map. Put ("3", 3 );
  6. Iterator itor = map. entryset (). iterator ();
  7. While(Itor. hasnext ()){
  8. Map. Entry <string, integer> entry = (Map. Entry <string, integer>) itor. Next ();
  9. System. Out. println ("Key =" + entry. getkey (). tostring ());
  10. System. Out. println ("values =" + entry. getvalue (). tostring ());
  11. }
       Map<String,Integer> map=new HashMap<String,Integer>();       map.put("1", 1);       map.put("2", 2);       map.put("3", 3);       map.put("3", 3);                Iterator itor=map.entrySet().iterator();       while(itor.hasNext()){         Map.Entry<String,Integer> entry=(Map.Entry<String,Integer>)itor.next();         System.out.println("key="+entry.getKey().toString());         System.out.println("values="+entry.getValue().toString());       }    

B: traverse hashtable and use the keyset and values interfaces.

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://www.javaeye.com/topic/421014# ">
  1. Set <string> keys = map. keyset ();
  2. Iterator itor = keys. iterator ();
  3. While(Itor. hasnext ()){
  4. Object key = itor. Next ();
  5. System. Out. println ("Key =" + key. tostring ());
  6. System. Out. println ("value =" + map. Get (key). tostring ());
  7. }
  8. Collection CO = map. Values ();
  9. Iterator itor = Co. iterator ();
  10. While(Itor. hasnext ()){
  11. System. Out. println ("value =" + itor. Next ());
  12. }
       Set<String> keys=map.keySet();       Iterator itor=keys.iterator();       while(itor.hasNext()){        Object key=itor.next();        System.out.println("key="+key.toString());        System.out.println("value="+map.get(key).toString());        }       Collection co=map.values();       Iterator itor=co.iterator();       while(itor.hasNext()){        System.out.println("value="+itor.next());        }       

Differences between hashtable and hashmap:
1. hashtable is a subclass of dictionary, and hashmap is an implementation class of the map interface;
2. Methods in hashtable are synchronized, while those in hashmap are not synchronized by default. That is to say, in multi-threaded applications, it is safe to use it without special operations.
Hashtable is used. For hashmap, an additional synchronization mechanism is required. However, the synchronization problem of hashmap can be solved through a static method of collections:
Map collections. synchronizedmap (MAP m)
This method returns a synchronous map, which encapsulates all the underlying hashmap methods, making the underlying hashmap safe even in a multi-threaded environment.
3. In hashmap, null can be used as a key, and there is only one such key. One or more keys can correspond to null values. When the get () method returns NULL, it can represent
If the key is not in hashmap, the corresponding value of the key can also be null. Therefore, the get () method cannot be used in hashmap to determine whether a key exists in hashmap. Instead
The containskey () method.

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.