Java_colletction application: conversion from hashmap to array

Source: Internet
Author: User

First, you must understand the following knowledge points:

1. collection is the interface of hashmap and array.

2. Elements in hashmap have no order, while arrays have

3. When hashmap is converted to an array object, the order of elements in it is the same as that when iterator is used to traverse the original hashmap.

4. The newly created array object corresponds to the list object in the parameter list; the collection object corresponds to the original hashmap (the so-called: the modification of an object will automatically change another object)

Next we will talk about how to convert a hashmap object to an array object.

First, convert the hashmap into a collection object.

Pass: values () method

Why can we do this:

Note that there is a member variable in the hashmap class:

Transient volatile collection <v> values = NULL;

Pay attention to the hashmap class, which has a member function:

Public Collection <v> values (){
Collection <v> Vs = values;
Return (! = NULL? VS: (values = new values ()));
}

The returned value is the value of the hashmap object.

Speaking of this, we should be aware that what you get now is just a collection object, and there is still one step away from our goal.

To convert a collection object into an array object, we use the following methods:

<T> T [] toarray (T [] );

It is necessary to describe this method. Here are some examples to verify my understanding.

First, let's take a look at the complaint about <t> T [] toarray (T [] A); in the source code of the collection interface:

(If the English version is acceptable, you are advised to directly read the original introduction. Because my English level is limited, the translation may not be ideal. Afraid of mistakes !)

/**
* Returns an array containing all of the elements in this collection;
* The runtime type of the returned array is that of the specified array.
* If the collection fits in the specified array, it is returned therein.
* Otherwise, a new array is allocated with the runtime type of
* Specified array and the size of this collection.
*
* <P> if this collection fits in the specified array with room to spare
* (I. e., the array has more elements than this collection), the element
* In the array immediately following the end of the collection is set
* <Tt> null </tt>. (This is useful in determining the length of this
* Collection <I> only </I> If the caller knows that this collection does
* Not contain any <tt> null </tt> elements .)
*
* <P> if this collection makes any guarantees as to what order its elements
* Are returned by its iterator, this method must return the elements in
* The same order.
*
* <P> like the {@ link # toarray ()} method, this method acts as bridge
* Array-based and collection-based APIs. Further, This method allows
* Precise control over the runtime type of the output array, and may,
* Under certain circumstances, be used to save allocation costs.
*
* <P> suppose <tt> x </tt> is a collection known to contain only strings.
* The following code can be used to dump the collection into a newly
* Allocated array of <tt> string </tt>:
*
* <PRE>
* String [] Y = x. toarray (New String [0]); </PRE>
*
* Note that <tt> toarray (new object [0]) </tt> is identical in function
* <Tt> toarray () </tt>.
*
* @ Param a array into which the elements of this collection are to be
* Stored, if it is big enough; otherwise, a new array of the same
* Runtime type is allocated for this purpose.
* @ Return an array containing all of the elements in this collection
* @ Throws arraystoreexception if the runtime type of the specified array
* Is Not A supertype of the runtime type of every element in
* This collection
* @ Throws nullpointerexception if the specified array is null
*/

 

This is probably the meaning of the above introduction:

This method returns a new queue object containing all elements in the collection object that calls this method; the type of elements in the new queue object is determined by the element type of the queue object in the parameter list. If the number of elements in the original collection object does not exceed the number of elements in the queue object in the parameter list, a new list object will not be created, instead, all the elements in the collection object are placed in the queue object in the parameter list. Otherwise, a queue object with the same element type as the queue object in the parameter list and the same number of elements as the original collection element will be created and assigned to it. In addition, if the number of queue object elements in the parameter list exceeds the original collection, the system will fill the remaining positions with the null element.

In addition, the order of elements in the new array object is the same as that in which an iterator is used to traverse the elements in the original collection object.

Like the toarray () method, this method converts a collection object to an array object. However, compared with the toarray () method, this method can more accurately control the element type of the new queue object and save memory under certain circumstances. Because, when the queue object in the parameter list is large enough, you do not need to apply for additional space to save the new queue object, but simply use the original space.

For example, X is a collection object that saves the string element. The following code shows how to use a queue object with a new allocated space to save a collection object:

String [] Y = x. toarray (New String [0]);

Note:

Toarray (new object [0]) and toarray () have the same effect.

*

*@ ParameterA specifies the element type of the newly converted queue object through the element type of queue object. If the element of queue object A is no less than the number of elements in the collection object to be converted, no additional memory space needs to be allocated. The elements of the original collection object are directly put into queue object.

*@ Returns a queue object that holds all elements of the original collection object.

*@ ThrowsArraystoreexception element type conflict

*@ ThrowsThe queue object in the nullpointerexception parameter list is empty.

Lab 1:

Purpose: To verify the number of elements in the queue object in the parameter list, and whether to re-allocate space and save New queue objects.

Source code 1:

Hashmap <string, string> test = new hashmap <string, string> ();
Test. Put ("A", "a's value ");
Test. Put ("B", "B's value ");
String [] test1 = new string [3];

String [] Test2 = test. Values (). toarray (test1 );

System. Out. println ("test1 :");
System. Out. println (test1.length );

For (string V: test1 ){

System. Out. println (v );

}
System. Out. println ("Test2 :");
System. Out. println (test2.length );
For (string V: Test2 ){

System. Out. println (v );

}

Console:

Test1:
3
B's Value
A's Value
Null
Test2:
3
B's Value
A's Value
Null

Analysis:

As shown above, both the queue object test1 In the parameter list and the target queue object Test2 point to the new queue object, that is, they do not need to allocate additional memory space in total. Because the number of elements in the queue object test1 In the parameter list exceeds the number of elements in the original collection object, the null element is entered later.


Source code 2:

Hashmap <string, string> test = new hashmap <string, string> ();
Test. Put ("A", "a's value ");
Test. Put ("B", "B's value ");
String [] test1 = new string [1];

String [] Test2 = test. Values (). toarray (test1 );

System. Out. println ("test1 :");
System. Out. println (test1.length );

For (string V: test1 ){

System. Out. println (v );

}
System. Out. println ("Test2 :");
System. Out. println (test2.length );
For (string V: Test2 ){

System. Out. println (v );

}

Console:

Test1:
1
Null
Test2:
2
B's Value
A's Value

Analysis:

As shown above, test1, the queue object in the parameter list, does not obtain any elements, but test2. It can be seen that a new memory space has been allocated.

Lab 2:

Objective: To find the correspondence between two queue objects. the correspondence between the collection object and the original hashmap object does not affect each other.

Source code:

Hashmap <string, string> test = new hashmap <string, string> ();
Test. Put ("A", "a's value ");
Test. Put ("B", "B's value ");
String [] test1 = new string [2];

Collection <string> variable = test. Values ();

String [] Test2 = variable. toarray (test1 );

Variable. Remove ("B's value ");

System. Out. println ("colletction object:" + variable. tostring ());
System. Out. println ("hashmap object:" + test );

// Test. Values (). toarray (test1 );
System. Out. println ("test1 :");
System. Out. println (test1.length );

For (string V: test1 ){

System. Out. println (v );

}
System. Out. println ("Test2 :");
System. Out. println (test2.length );
For (string V: Test2 ){

System. Out. println (v );

}
Test2 [0] = "kaiwii modify! ";
System. Out. println ("Test2 :");
System. Out. println (test2.length );
For (string V: Test2 ){

System. Out. println (v );

}
System. Out. println ("test1 :");
System. Out. println (test1.length );

For (string V: test1 ){

System. Out. println (v );

}
System. Out. println (test. tostring ());
System. Out. println ("whether test contain the modified value:" + test. containsvalue ("kaiwii modify! "));

Console:

Colletction object: [A's value]
Hashmap object: {A = A's value}
Test1:
2
B's Value
A's Value
Test2:
2
B's Value
A's Value
Test2:
2
Kaiwii modify!
A's Value
Test1:
2
Kaiwii modify!
A's Value
{A = A's value}
Whether test contain the modified value: false

 

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.