Two ways to traverse a map: Keyset () and EntrySet (), and ArrayList Toarray__java

Source: Internet
Author: User
Keyset is the set of keys, the type of the set is the type of key
EntrySet is a set of key-value pairs, and the type within set is Map.entry
1.keySet ()
Map map=new HashMap ();
Iterator It=map.keyset (). iterator ();
Object key;
Object value;
while (It.hasnext ()) {
key=it.next ();
Value=map.get (key);
System.out.println (key+ ":" +value);
}
2.entrySet ()
Map map=new HashMap ();
Iterator It=map.entryset (). iterator ();
Object key;
Object value;
while (It.hasnext ()) {
Map.entry Entry = (map.entry) it.next ();
Key=entry.getkey ();
Value=entry.getvalue ();
System.out.println (key+ "=" +value);
}

Keyset is the set of keys, the type of the set is the type of key
EntrySet is a set of key-value pairs, and the type within set is Map.entry
The speed of keyset () is much slower than EntrySet ().
Using EntrySet, you must convert the map object to Map.entry,keyset you do not need

If you want to convert a list directly into an object array, you can use the object[] o = List.toarray () directly;

If you want to convert to a string array, you have the following two ways:

Method one, string[] arr = new String[list.size]; List.toarray (arr)//This time arr has the value of the list

Method II, string[] arr = (string[]) List.toarray (new string[0))


List List = new ArrayList ();
   List.add (New Person ());
   List.add (New Person ());
   List.add (New Person ());
   Person[] person = (person[]) List.toarray ();

Running the program throws an exception: Java.lang.ClassCastException

But run the following code:

List List = new ArrayList ();
   List.add (New Person ());
   List.add (New Person ());
   List.add (New Person ());
   Person[] person = (person[]) List.toarray (new person[0));

The program executes correctly,

Reason, first look at the ArrayList two ToArray () method of source code:

Public object[] ToArray () {
    object[] result = new Object[size];
    System.arraycopy (elementdata, 0, result, 0, size);
    return result;
}
Public object[] ToArray (Object a[]) {
    if (A.length < size)
        a = (object[)) java.lang.reflect.Array.newInstance (A.getclass (). Getcomponenttype (), size);
    System.arraycopy (elementdata, 0, a, 0, size);
    if (a.length > Size)
        a[size] = null;
    return A;
}

It can be seen that the ToArray method without parameters is constructed an object array, and then copy the data, at this time the transformation will produce classcastexception with parameters

ToArray method, it constructs an empty array of the corresponding type, which is consistent with the size of the ArrayList, according to the type of the parameter array, although the method itself is still an array of object

, but because the componenttype of the constructed array is consistent with the componenttype that needs to be transformed, there is no transition anomaly.

So, what is the special meaning of the ToArray method with no parameters? or speak in code:

ArrayList list = new ArrayList ();
List.add (New Person ());
List.add (New Person ());
List.add (New Person ());
There is no need to transform, nor to use transformation
 object[] ss = List.toarray ();
This can be transformed to take out the object for the original ArrayList
(int i = 0; i < ss.length; i++) {person
person= (person) ss[i];
SYSTEM.OUT.PRINTLN (person);
}
Is that, if you use ToArray, you need to "manually" and "individually" for type conversion.

Here's what you write when you use it:
var newdata={};
	function Getformdata (newdata) {var formdatas = {};
		<% map<string,string> formidmap_new=new hashmap<string,string> ();
		 formidmap_new= (hashmap<string,string>) session.getattribute ("Formid_b");
		 String[] Keys_b_new=formidmap_new.keyset (). ToArray (new string[0));
			  for (int i=0;i<keys_b_new.length;i++) {%> var key= ' <%=keys_b_new[i]%> ';//Console.log (key);    
             var row = new Object ();
			 var jsvalue = $ (' #<%=keys_b_new[i].substring (21,keys_b_new[i].length ())%> '). html (); if (jsvalue!= undefined) {jsvalue = Jsvalue.replace ("<br>", ""). Replace ("</br>", ""). Replace ("<BR>"
             , ""). Replace ("</BR>", "")} var mapkey= "<%=keys_b_new[i].substring (21,keys_b_new[i].length ())%>";
             Newdata[mapkey]=jsvalue;	
			Formdatas[key]=jsvalue;
			 /* IF (Key.indexof ("B1_d6") >0) {alert (newdata[mapkey]+ "NEW55"); } *///console.log (Jsdatamap[key);//The result is 1.
	<%}%> return formdatas;
 }




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.