To elaborate on two iterations of map in Java

Source: Internet
Author: User

The iterative approach to Java has always been a blur, and today it's finally understood. Deliberately summed up a bit. Basically it's a thorough understanding.

1. Say iterator before you go to map:

Iterator is primarily used to traverse (that is, iterate) the elements in the collection collection. Iterator is also called an iterator. It is only three methods: Hasnext (), Next () and remove ()

Hasnext (): Returns trueif there are still elements that can iterate. In other words It returns trueif next returns an element instead of throwing an exception.

Next (): Returns the next element of the iteration.

Remove (): Removes the last element returned by the iterator from the collection that the iterator points to (optional action). Each call to next can only call this method once. The behavior of the iterator is indeterminate, assuming that the collection that the iterator points to is modified by calling this method in an iterative way.

Note: The iterator must be attached to the collection object. Suppose you have a iterator object. There must be a collection object associated with it.

When iterating over the elements in a collection using iterator, iterator does not pass the collection element itself to the iteration variable, but instead passes the value of the collection element to the iteration variable, so changing the value of the iteration variable has no effect on the collection element itself.

There are two main iterations of the 2.Map (Key,value) Collection:

(1) Put back a set set by calling keyset. This collection holds the key, then iterates over the set set to get the key value, and then returns to the map for querying based on the resulting key value.

(2) by calling EntrySet (), a set set of entry objects is returned, and the set collection is iterated to get the entry object, and then the key and value values are obtained through Entry.getkey () and Entry.getvalue ().

Two ways to understand the diagram:



Java code example:


Package cn.wwh.www.mapexercise;


Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.Map.Entry;


Import Org.junit.Test;


/**
* The role of the class: mainly used to test the map of the two ways of iteration
*
*
* @author Skiff
* @version 1.0
* @ creation time: 2014-7-19 7:10:35
*/


public class Demo1 {


@Test
public void TestMap1 () {


Map<integer, string> map1 = new Hashmap<integer, string> ();
for (int i = 0; i < i++) {
Putting data into the collection
Map1.put (i *, "testData" + i);
}
Call Keyset () returns a set set
set<integer> DataSet = Map1.keyset ();
iterator<integer> dataite = Dataset.iterator ();
while (Dataite.hasnext ()) {
To iterate from the set set to get a key value
Integer key = Dataite.next ();
The corresponding value is obtained according to the key value
String value = Map1.get (key);
SYSTEM.OUT.PRINTLN (key + "----->" + value);
}
SYSTEM.OUT.PRINTLN ("Total co-ownership" + map1.size () + "one");


}


@Test
public void TestMap2 () {


Map<integer, string> map2 = new Hashmap<integer, string> ();
for (int i = 0; i < i++) {
Putting data into the collection
Map2.put (i *, "testData" + i);
}
Note that this Entry is java.util.Map.Entry. Guide Pack do not error
Set<entry<integer, string>> entryset = Map2.entryset ();
Iterator<entry<integer, string>> entryite = Entryset.iterator ();
while (Entryite.hasnext ()) {
A entry is returned by iteration.
Entry<integer, string> Entry = Entryite.next ();
Integer key = Entry.getkey ();
String value = Entry.getvalue ();
SYSTEM.OUT.PRINTLN (key + "----->" + value);
}


SYSTEM.OUT.PRINTLN ("Total co-ownership" + map2.size () + "one");


}


}


TESTMAP1 () test result diagram:


TESTMAP2 () test result diagram:



To elaborate on two iterations of map in Java

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.