"Java" uses a new array traversal method after JDK1.5 to traverse HashMap, HashMap should not store multivariate groups

Source: Internet
Author: User

In the JDK1.5 there is a for loop of the traversal of the wording, now should no one use JDK1.4 it? I see those 2005 Java Books, rectification series of Java books are used JDK1.5, of course JDK1.7 has been used, it is said that JDK1.7 is compatible jdk1.2~jdk1.7, now Java programming is based on JDK1.5. However, because the exam is not required or any other reason, it is not favored by the program Ape, and in the ordinary practice, the old-fashioned cycle used more, the older ape still as the development of the main situation, there is no one dares to try this new type of array traversal method, in fact, this method is particularly useful when traversing hashmap, Can write a lot less code, the idea is clear. A bit similar to the Foreach method in PHP.

What is the new array traversal method after JDK1.5?

Let's take an example of the simplest topic, define an array, ask for the sum of the elements inside, and ask for the maximum (small) value inside.

The person who has learned Java must be a compulsory course, which is generally written like this:

public class Arrtest {public static void main (string[] args) {int[] arr = {1, 12, 32, 13, 23, 24, 21, 4, 214, 21, 5, 12} ; int arrtotal = 0;int max = arr[0];for (int i = 0; i < arr.length; i++) {int element = Arr[i];arrtotal + = element;if (M Ax < element) {max = element;}}}}

The experimental results are as follows:


But in fact, the new array traversal method after using JDK1.5 can be written like this:

public class Arrtest {public static void main (string[] args) {int[] arr = {1, 12, 32, 13, 23, 24, 21, 4, 214, 21, 5, 12} ; int arrtotal = 0;int max = arr[0];for (int element:arr) {arrtotal + = element;if (Max < element) {max = element;}} System.out.printf (arrays.tostring (arr)); System.out.print (the sum of all elements in the \ n array is: "+ arrtotal +" \ n "); System.out.println ("The maximum value in the array is:" + max);}}

The experimental results are the same as above,

The key change is the condition inside the for loop, which means defining the element of the I in ARR as elements, where I is 0 to arr.length and automatically self-increment

This avoids the definition of the I variable, and then write a self-increment of I, in fact, the for loop is the improvement of the while loop, which often defines an i outside the while loop, and then the while inside the self-increment, for a bracket is OK, now this new type for loop appears, Let this I also hide out. This is useful in some cases, such as finding the maximum now, the sum of the elements in the array, and so on, but it does not mean that the new for loop can replace the for, after all, once I becomes important, for example, to find the position of the largest of the arrays, bubble sort to manipulate the elements of element I, etc. This foreach loop is finished.

At the same time, please look at my code above, foreach in the traversal of the output of the entire array, not good, after all, after each element "" or "," can not eliminate, for example, the use of the foreach output array elements, is always 1, 12, 32, 13, 23, 24, 21, 4, 214, 21, 5, 12 1 12 32 13 23 24 21 4 214 21 5 12, the last element always exists one, or a space, the output is not good to see, it is better to use my "Java" arrays in the int array method, the integer array and the INT Array (click the Open link) to propose a method, using System.out.print (arrays.tostring (arr)), to output an array of the actual.


Second, how to traverse HashMap?

Output a HashMap is OK, directly can be used System.out.println (HASHMAP), output, but it is necessary for you to each key in the HASHMAP and the corresponding elements of each key to deal with, this is a big head. Encapsulation method with more, let you deal with the inside of the deep things, oh, now the framework of what prevails, all this.

You can actually use this foreach to traverse the HashMap, as follows:

Import java.util.*;p Ublic class Hashmaptraversal {public static void main (string[] args) {hashmap<string, string> s Tring_map = new hashmap<string, string> () string_map.put ("Key1", "value1"), String_map.put ("Key2", "value3"); String_map.put ("key121", "value3211"), String_map.put ("Key31", "Value12"), String_map.put ("key41", "Value10"); for ( String Key:string_map.keySet ()) {System.out.println ("key:" + key + "corresponds to the value:" + string_map.get (key));}}}

First define a hashmap, put 5 elements inside, and then use String_map.keyset () to take out this String_map key collection, the String_map key set of the foreach traversal, set key is String_ The first element of the map key collection, and then use the Get method to take out the inside of the set on it. The experimental results are as follows:


As for those hashmap<string, what about arraylist<integer,string>>? This is not a good way to store data to process, I in the "Java" Java Collections class--java in the data structure of the upgrade (click Open link) mentioned, this method is wrong, since you want to store a ternary group, why do not define a class, For example:

Class Tuple{public string Key;public int integer;public string string;}
And then in defining a dynamic array that holds this multivariate tuple class ArrayList, does it have to complicate the whole hashmap?

For example:

Arraylist<tuple> tuple_arraylist=new arraylist<tuple> ();

C language will define the structure, and then come to Java in merely hashmap, this is really unwise!

"Java" uses a new array traversal method after JDK1.5 to traverse HashMap, HashMap should not store multivariate groups

Related Article

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.