Ways to implement traversal hashmap in multiple ways _java

Source: Internet
Author: User
Tags iterable set set

Today's main explanation is to use a variety of ways to implement traversal hashmap to remove key and value, first in Java if you want a collection to be able to use for enhancements to implement the iteration, then this interface or class must implement the Iterable interface, So iterable is how to achieve the iteration, here will not do the explanation, the following main explain the traversal process.

Defines a generic set
map<string, string> Map = new hashmap<string, string> ();
Add data to the collection through the put method of the map
    map.put ("001", "Liu Bei");
    Map.put ("002", "Caocao");
    Map.put ("003", "Sun Quan");

Mode one: Using the keyset method of the map interface to implement

Question: We all know that the map interface does not implement the Iterable interface, why is it possible to use his Ketset method to implement the iteration?

Resolution: Because the Keyset method returns a set view of the keys contained in this map, this method can return a set attempt, and it says that his return value type is a set interface, and we can see the set interface through the API document, and he implements the Iterable interface, so it can implement iterations.

Call the keyset method to put back a set interface type
    set<string> set = Map.keyset ();
    Use for enhancement to remove key and value for
    (String item:set) {
      System.out.println ("key is:" + item +); Value is: "+ map.get (item));
    }

Mode two: Implementing the values method using the Map interface (for enhancements)

Similarly: The values method that invokes the map interface he put back a collection attempt, collection interface He also implemented the Iterable interface, so can iterate.

collection<string> con = map.values ();
    for (String Item:con) {
      System.out.println ("value is:" + Item);
    }

Mode III: Implementation using the EntrySet method of the map interface (for enhancements)

The return value of the Entryset:entryset () also returns a set set, the type of which is map.entry,map.entry is an internal interface of the Map declaration, and this interface is generic, defined as entry<k,v>. It represents an entity (a Key-value pair) in a map.

set<entry<string, string>> setentry = Map.entryset ();
    For (entry<string, string> item:setentry) {
      System.out.println ("key is:" + item.getkey () +); Value is: "
          + item.getvalue ());
    }

Mode four: Keyset () using the map interface. Iterable () (While loop)

Iterable (): Returns an iterator that iterates over the elements in this set. The returned element is not in a specific order (unless this set is an instance of a class that provides an order guarantee). return value type Iterator<e>

Iterator<string> it = Map.keyset (). iterator ();
Returns true if there are still elements that can be iterated. While    (It.hasnext ()) {     //Get key value
   String key = It.next ();   System.out.println ("Key" is: "+ key +"; Value is: "+ map.get (key));
   }

Mode five: Value.iterable () with the map interface (while loop)

Iterator<string> it1 = Map.values (). iterator ();
    while (It1.hasnext ()) {
      String value = It1.next ();
      System.out.println ("value is:" + value);
    }

Mode VI: EntrySet () using the map interface. Iterable () (While loop)

iterator<entry<string, string>> it2 = Map.entryset (). iterator ();
    while (It2.hasnext ()) {
      entry<string,string> entry=it2.next ();
      System.out.println ("Key is:" + entry.getkey () + "; Value is: "+ entry.getvalue ());
    }

The above method of using a variety of ways to achieve traversal HashMap is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.