Traversal of the Java map Collection (brief)

Source: Internet
Author: User
Tags set set

Java Map Traversal of a collection (brief)

Let's share it today. Iterator traversal and enhanced for traversal for collections (take the map collection as an example)

To everyone simple introduction to the map collection, the map collection is stored in the key value pairs, and set, ArrayList and other sets of different is it does not include Add method, such as adding elements, the details are not exhausted, to use when you can go directly to search. It uses put and remove to delete elements, the elements inside is unordered, is a thread unsafe collection. The following code is an example:

Private Static Map Inputkeyandvalue () {

TODO auto-generated Method stub

map<integer,string> map = new hashmap<integer,string> ();

Map.put (1, "a");

Map.put ("B");

Map.put (6, "C");

Map.put (5, "a");

System. out. println (map);

System. out. println (Map.get ("66"));

return map;

}

Remove the elements from the map collection using remove in the Delete method (delete the key value pairs by key)

map<integer,string> recive_delete = inputkeyandvalue();

Recive_delete.remove (1);

You can then start traversing the map collection (using keyset traversal), call the map's keyset method, store the data in the set collection, iterate through the iterator, or enhance the for traversal traversal, with the following code:

Call the map's keyset method to store the data in the set collection, keyset traversal mode

set<integer> set = Recive_mapset.keyset ();

Iterate through the set with an iterator

System. out. println ("iterator traversal");

Iterator<integer> it = Set.iterator ();

while (It.hasnext ()) {

Integer key = It.next ();

String value = Recive_mapset.get (key);

System. out. println (key+ "..." +value);

}

System. out. println ("Enhanced for Traversal");

for (Integer Key:set) {

String value = Recive_mapset.get (key);

System. out. println (key+ "..." +value);

}

The second traversal method, called the map's entry method (entry represents the relationship between key-value pairs), the key-value pairs of the relationship to the set set, Traverse entry, and then call entry Getkey and GetValue methods to get the keys and values. The code is as follows.

The relationship between the key-value pairs is stored in the set collection, and the EntrySet traversal mode

set<map.entry<integer,string>> set = Recive_mapset.entryset ();

Enhanced for .....

Iterators

Iterator<map.entry<integer,string>> it = Set.iterator ();

while (It.hasnext ()) {

System.out.println (It.next ());

map.entry<integer,string> Entry = It.next ();

Integer key = Entry.getkey ();

String value = Entry.getvalue ();

System. out. println (key + "..... +value");

}

Note: I built the inputkeyandvalue() method in the Java project to create a map collection that calls the method in different methods to generate the Map collection Object Recive_mapset

The source code is attached below:

Package day20;

import Java.util.HashMap;

import java.util.Iterator;

import Java.util.Map;

import Java.util.Set;

Public class Mapdemo {

Public Static void Main (string[] args) {

inputkeyandvalue();

Delete ();

mapset();

EntrySet ();

}

Private Static void EntrySet () {

TODO auto-generated Method stub

map<integer,string> recive_mapset = inputkeyandvalue();

The relationship between the key-value pairs is stored in the set collection, and the EntrySet traversal mode

set<map.entry<integer,string>> set = Recive_mapset.entryset ();

Enhanced for .....

Iterators

Iterator<map.entry<integer,string>> it = Set.iterator ();

while (It.hasnext ()) {

System.out.println (It.next ());

map.entry<integer,string> Entry = It.next ();

Integer key = Entry.getkey ();

String value = Entry.getvalue ();

System. out. println (key + "..... +value");

}

}

Private Static void mapset () {

TODO auto-generated Method stub

//

map<integer,string> recive_mapset = inputkeyandvalue();

Call the map's keyset method to store the data in the set collection, keyset traversal mode

set<integer> set = Recive_mapset.keyset ();

Iterate through the set with an iterator

System. out. println ("iterator traversal");

Iterator<integer> it = Set.iterator ();

while (It.hasnext ()) {

Integer key = It.next ();

String value = Recive_mapset.get (key);

System. out. println (key+ "..." +value);

}

System. out. println ("Enhanced for Traversal");

for (Integer Key:set) {

String value = Recive_mapset.get (key);

System. out. println (key+ "..." +value);

}

}

Private Static void Delete () {

TODO auto-generated Method stub

map<integer,string> recive_delete = inputkeyandvalue();

Recive_delete.remove (1);

System. out. println (Recive_delete);

}

Private Static Map Inputkeyandvalue () {

TODO auto-generated Method stub

map<integer,string> map = new hashmap<integer,string> ();

Map.put (1, "a");

Map.put ("B");

Map.put (6, "C");

Map.put (5, "a");

System. out. println (map);

System. out. println (Map.get ("66"));

return map;

}

}

If there is an incorrect place, please correct me, thank you for watching.

Traversal of the Java map Collection (brief)

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.