Use of the Map set framework and the Map set framework

Source: Internet
Author: User

Use of the Map set framework and the Map set framework

 

Map is used to save data with ing relationships (key-vlaue ). Duplicate Map keys are not allowed. That is, if two keys of the same Map object are compared using the equals method, false is always returned.

Map contains a keySet () method, which is used to return a Set composed of Map and key.

The Map Set and the Set element are stored in a similar way. For example, the Set Interface contains implementation classes and subinterfaces, such as HashSet, javashashset, SortedSet (Interface), TreeSet, and EnumSet, in the Map interface, there are implementation classes and subinterfaces such as HashMap, LinkedHashMap, SortedMap, TreeMap, and EnumMap.

 

The value of Map is very similar to List: Elements and elements can be repeated. Each element can be searched by index (key.

Map is sometimes called a dictionary or an associated array.

The Map interface defines the following methods:

  • Void clear (); Delete all key-value pairs in the Map object.
  • Boolean containsKey (Object key): queries whether the specified key is included in the Map. If the key is included, true is returned.
  • Boolean containsValue (Object value): queries whether Map contains one or more values. If it contains values, returns true.
  • Set entrySet (): returns a Set composed of all key-value pairs in the Map. Each Set element is a Map. Entry (Entry is an internal class of Map) object.
  • Object get (Obejct key): returns the value corresponding to the specified key. If the Map does not contain the key, null is returned.
  • Boolean isEmpty (): checks whether the Map is null (that is, it does not contain any key-value pairs). If it is null, true is returned.
  • Set keySet (): returns the set Set composed of all keys in the Map.
  • Object put (Object key, Object value): Add a key-value pair. If the current Map already has a key-value Pair equal to the key, the new key-value Pair overwrites the original key-value pair.
  • Object remove (Object key): deletes the key-value pair corresponding to the specified key, and returns the value associated with the deleted key. If the key does not exist, null is returned.
  • Int size (): returns the number of key-value pairs in the Map.
  • Collection values (): returns the Collection composed of all values in the Map.

The Map interface provides a large number of implementation classes, such as HashMap and Hashtable, as well as the HashMap subclass LinkedHashMap, as well as the SortedMap sub-interface and the implementation class TreeMap of this interface. The following is a detailed introduction.

Map contains an internal class: Entry. This class encapsulates a key-value pair. The Entry contains three methods:

  • Object getkey (): return the key value contained in the Entry.
  • Object getValue (): returns the value contained in the Entry.
  • Object setValue (): set the value contained in the Entry and return the new value.

Map can be understood as a special Set, but the Set element in the Set is an Entry object rather than a common object.

 

1. Implementation class of HashMap and Hashtable

Both HashMap and Hashtable are implementation classes of the Map interface. Hashtable is an ancient Map implementation class that has been available since JDK1.0. It contains two cumbersome Methods: elements () (similar to the values () method defined by the Map interface) and keys () (similar to the keySet () method defined by the Map interface), these two methods are rarely used.

Two differences:

  • Hashtable is a thread-safe Map implementation, but HashMap is a thread-safe implementation, so HashMap has higher performance than Hashtable. However, if multiple threads access the same Map object, it is better to use Hashtable to implement the class.
  • Hashtable does not allow null as the key and value. If it is null, NullPointerException is thrown. However, HashMap can use null as the key or value.

Because HashMap can be unique, only one key-value Pair in HashMap can be null, but there can be countless key-value pairs whose values are null.

HashMap overwrites the toString () method and always returns a string in the following format: {key1 = value1, key2 = value2 ..}

HashMap and Hashtable determine that two keys are equal: two keys use the equasl method to compare and return true, and the hashCode values of the two keys are equal.

LinkedHashMap class

HashMap has a subclass: LinkedHashMap, which is also a two-way linked list to maintain the order of key-value pairs. This Linked List defines the iteration sequence, the iteration sequence is consistent with the insertion sequence of key-value pairs.

LinkedHashMap can avoid sorting key-value pairs in HashMap and Hashtable (as long as the key-value pair is inserted in sequence ). At the same time, you can avoid the increased cost of using TreeMap.

LinkedHashMap needs to maintain the insertion sequence of elements, so its performance is slightly lower than that of HashMap. However, it performs well when iteratively accessing all elements in Map, because it maintains the internal order by using a linked list.

Properties class

The Properties class is a subclass of the Hashtable class and is used to process property files (such as INI files on Windows operating platforms ). The Properties class can associate the Map object with the attribute file, so that the key-value pair in the Map object can be written to the attribute file, you can also load the property name = property value in the property file to the Map object. Because the attribute names and attribute values in the property file can only be strings, the keys and values in Properties are strings, this class provides the following three methods to modify the key and value values in Properties.

Two methods are provided to read and write attribute files:

--- Restore content end ---

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.