Java traversal of Map method set instances

Source: Internet
Author: User
Tags concurrentmodificationexception

Map is a data storage method. In my personal opinion, it is an array in php or asp.net and can be directly used for Traversing arrays. However, there are many inherent methods for Traversing map in java, let's take a look at some instances.

The traversal Map method can be divided into two types:

One type is map-based Entry; map. entrySet ();

One is map-based key; map. keySet ()

Each category has two traversal methods:

A. Use the iterator;

B. Use the for-each loop;


The first method is to obtain the set of keys based on the map's keyset () method, and then traverse the map to get the value of the value.

The Code is as follows: Copy code

Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Set;

Public class HashMapTest2
{
Public static void main (String [] args)
{
HashMap map = new HashMap ();

Map. put ("a", "aaaa ");
Map. put ("B", "bbbb ");
Map. put ("c", "cccc ");
Map. put ("d", "dddd ");

Set set = map. keySet ();

For (Iterator iter = set. iterator (); iter. hasNext ();)
{
String key = (String) iter. next ();
String value = (String) map. get (key );
System. out. println (key + "===" + value );
}
}
}

 

The second method is to use Map. Entry for obtaining:

The Code is as follows: Copy code

Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Map;
Import java. util. Set;

Public class HashMapTest4
{
Public static void main (String [] args)
{
HashMap map = new HashMap ();

Map. put ("a", "aa ");
Map. put ("B", "bb ");
Map. put ("c", "cc ");
Map. put ("d", "dd ");

Set set = map. entrySet ();

For (Iterator iter = set. iterator (); iter. hasNext ();)
{
Map. Entry entry = (Map. Entry) iter. next ();

String key = (String) entry. getKey ();
String value = (String) entry. getValue ();
System. out. println (key + ":" + value );
}
}
}

Traversal method 3 treemap keySet () Traversal

The Code is as follows: Copy code

System. out. println ("method 4 ");
For (Object o: tempMap. keySet ()){
System. out. println ("key =" + o + "value =" + tempMap. get (o ));
}
System. out. println ("11111 ");

// How does java traverse Map <String, ArrayList> map = new HashMap <String,
// ArrayList> ();
System. out
. Println ("java traversal Map <String, ArrayList> map = new HashMap <String, ArrayList> ();");
Map <String, ArrayList> map = new HashMap <String, ArrayList> ();
Set <String> keys = map. keySet ();
Iterator <String> iterator = keys. iterator ();
While (iterator. hasNext ()){
String key = iterator. next ();
ArrayList arrayList = map. get (key );
For (Object o: arrayList ){
System. out. println (o + "traversal Process ");
}
}
System. out. println ("2222 ");
Map <String, List> mapList = new HashMap <String, List> ();
For (Map. Entry entry: mapList. entrySet ()){
String key = entry. getKey (). toString ();
List <String> values = (List) entry. getValue ();
For (String value: values ){
System. out. println (key + "-->" + value );
}
}
}
}


Deletes a specified element while traversing the Map.

The Code is as follows: Copy code

Package net. nie. test;

Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Map;

Public class HashMapTest {
Private static Map <Integer, String> map = new HashMap <Integer, String> ();

/** 1. The HashMap class ing does not guarantee the sequence. Some mappings can clearly ensure the sequence: TreeMap class
* 2. During Map traversal, map. put (key, newVal) and map. remove (key) cannot be used to modify or delete elements,
* Concurrent modification exceptions may occur. You can use the iterator remove ():
* Remove the elements of the current iteration from the collection to which the iterator points
* To delete access elements.
**/
Public static void main (String [] args ){
Map. put (1, "one ");
Map. put (2, "two ");
Map. put (3, "three ");
Map. put (4, "four ");
Map. put (5, "five ");
Map. put (6, "six ");
Map. put (7, "seven ");
Map. put (8, "eight ");
Map. put (5, "five ");
Map. put (9, "nine ");
Map. put (10, "ten ");
Iterator <Map. Entry <Integer, String> it = map. entrySet (). iterator ();
While (it. hasNext ()){
Map. Entry <Integer, String> entry = it. next ();
Int key = entry. getKey ();
If (key % 2 = 1 ){
System. out. println ("delete this:" + key + "=" + key );
// Map. put (key, "odd"); // ConcurrentModificationException
// Map. remove (key); // ConcurrentModificationException
It. remove (); // OK
}
}
// Traverse the current map. This new for loop cannot modify the map content because it does not pass the iterator.
System. out. println ("------- nt final map element traversal :");
For (Map. Entry <Integer, String> entry: map. entrySet ()){
Int k = entry. getKey ();
String v = entry. getValue ();
System. out. println (k + "=" + v );
}
}
}

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.