Dark Horse programmer-java Basic---set frame-map

Source: Internet
Author: User
Tags set set

First Lecture Map Overview & sub-class object features & Common Methods

1. Definition

Map Collection: The collection stores key-value pairs, a pair of pairs, and the key is guaranteed to be unique. If a duplicate key is stored, the value corresponding to this key will be overwritten by the next one.

2, Map sub-class object characteristics

The Map collection has the following three sub-classes:

L Hashtable: The underlying is a hash table data structure that cannot be stored in a null key or null value. Thread synchronization, JDK1.0, is slightly less efficient.

L HASHMAP: The underlying is a hash table data structure that can be stored in a null key or null value. Thread synchronization, JDK1.2 appear, high efficiency.

L TREEMAP: The bottom layer is a two-fork tree data structure, threads are out of sync and can be used to sort the map collection.

3. Function of operation data in map collection

1) Adding elements

L put (K key, V value)--a key value pair is stored in the map collection, if the same key already exists in the collection, returns the value corresponding to the key, or null if there is no same key in the collection.

L Putall (map<? Extends K,? Extends v> m)--stores the key values in a map collection in another map collection

2) Delete

L Clear ()--Clears the map collection;

L Remove (Object key)-delete the key corresponding to the element, and return the value;

3) Interpretation

L Containsvalue (Object value)--Determine whether the corresponding element exists in the map collection by value;

L ContainsKey (Object Key)--press the key to determine if there is a corresponding element in the map collection;

L IsEmpty ()--determine if the map set is empty;

4) Get

Get (Object key)--Gets the elements in the map collection according to the key;

Size ()--Gets the number of elements in the map collection;

VALUES ()--Get all the values in the Map collection

The code is implemented as follows:

Second lecture Map two ways to remove a collection: KeySet and the EntrySet

1. Get the idea of the elements in the map collection: The method get () that gets the value according to the key in map, and if we can get all the keys in the collection, we can use the Get () method to get the value in the collection.

2. Use the keyset method to remove elements from the map collection

Idea: The keyset method can get all the keys in the collection and put those keys into the set collection. We can then use the iterator to read the keys in the set and get () the value corresponding to each key.

The code is implemented as follows:

3. Use the EntrySet method to remove elements from the map collection

idea: the mapping Relationship (map.entry<k,v>) in the map collection can be obtained through the EntrySet () method and stored in the set set. Gets the keys and values in the relationship by mapping the Getkey () and GetValue () methods in the relational interface map.entry<k,v>.

Map.entry Description: Entry is also an interface that is an internal interface in the map interface and is static and can be called directly through an external interface. Note: Internal interfaces are implemented by internal classes.

The code is implemented as follows:

4. Practice

Title: Each student has a corresponding attribution, the student attributes are: name, age, when the name and age are regarded as the same student. To ensure the uniqueness of students

Ideas:

L describe students;

L Define the map container, the student composition key, address as a value, deposited into the container;

L GET the elements in the map collection;

The code is implemented as follows:

5. Map Set Extension

When a key and value is not a one-to-many relationship, but multiple objects have a key-value pair relationship, they need to be stored in the form of a nested map collection.

Workaround: You can encapsulate multiple objects into a map collection, and then save the collection and key to the outside map collection.

The code is implemented as follows:

Summary of Knowledge points

1. The keys and values in the map are a one-to-a-relationship, and the keys must be unique in the map. If you have a one-to-many relationship, you can store it using a nested map collection, that is, put multiple objects in the embedded map collection first, and then save the collection in another map collection.

2. There are three ways to get all the values in the Map collection

L collection<v> values ();

L Keyset Way

Idea: Get the Map collection key first and then take the value based on the key.

L EntrySet Way

Idea: Get the mapping in the Map collection, then use the acquired mapping to get the values in the Map collection.

Dark Horse programmer-java Basic---set frame-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.