A collection of Java (vii) MAP

Source: Internet
Author: User

Reprint please indicate source: http://www.cnblogs.com/lighten/p/7327216.html

1. Preface

In order, this chapter is to introduce and explain the related classes of set. But for its implementation to know that the code of the set itself is very simple, its implementation is through the map to achieve, so this chapter describes the Java map interface and abstract class.

2.Map interface

Except for the new features added by JDK8, the method defined in map is as follows:

1.size (): Returns the number of key-value pairs

2.isEmpty (): Determines whether it is empty

3.containsKey (Key): whether the given key exists in the collection of keys

4.containsValue (value): whether the given value exists in the collection of values

5.get (Key): Gets the value of the specified key

6.put (key,value): Deposit a pair of key values

7.remove (key): Removes key-value pairs for the specified key

8.putAll (MAP): deposit all the given key value pairs

9.clear (): Clears all key-value pairs

10.keySet (): Returns all key values in the Set collection

11.values (): Returns a collection of all values

12.entrySet (): Returns a set set of key-value pairs

13.getOrDefault (): Returns the value if the key exists (even if the corresponding value is null), otherwise the given default value is returned

14.putIfAbsent (): If the given key does not exist or if there is a corresponding value of NULL, reset it, or return its value

15.remove (Key,value): If the given value is not equal to the value of the key, the key is not removed.

16.replace (Key, OldValue, NewValue): If the key corresponds to a value equal to the old value, replace it with the new value.

17.replace (key, value): If the key exists, replace it with the new value. and returns the previous value.

All of the above methods, from Getordefault, use the new feature of JDK8, which can implement the default method in the interface.

3.Entry

Entry is a key data structure implemented by map (that is, a key-value pair object), and the interface definition for entry is as follows:

1.getKey (): Gets the key for the key-value pair

2.getValue (): Gets the value of a key-value pair

3.setValue (): Sets the value of a key-value pair

The following methods are JDK8 related content, not to be explained.

4.AbstractMap

The implementation of the abstract class does not have much reference value, just like other collections, most subclasses cover the associated methods of the abstract parent class.

The size method is quite interesting. It uses the size of the EntrySet, and the opening also says that the set is implemented using map, which actually calls the map size () method. If the abstract parent is written like this, then the loop is dead. There is also the clear method.

Containsvalue or key are all implemented by EntrySet to get all the key values, and the implementation is basically consistent with the abstract parent class of the other sets. After the Get,remove and other methods are the same principle. The rest of the methods are no longer one by one narrative, if the previous chapters set the class seriously studied, this basic is the same routine.

A collection of Java (vii) MAP

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.