Java Collection Map&hashmap

Source: Internet
Author: User

Http://blog.csdn.net/u011240877/article/category/6447444/1

The map interface provides three angles to analyze the map:

  • Keyset:keyset is a collection of key (key) in a Map, saved as set , and not allowed to be duplicated, so the objects stored by the key need to override the Equals () and Hashcode () methods. Can be obtained through the Map.keyset () method.
  • Using KeySet traversal:
  • Set set = map.keySet();    for (Object key : set) {        System.out.println(map.get(key));    }
  • Values:values is a collection of MAP values (value) that is saved as Collection and therefore can be duplicated. Can be obtained through the Map.values () method.
  • Use the values traversal:
  •  Collection values = map.values();
     Iterator iterator = values.iterator(); while (iterator.hasNext()){ System.out.println("value " + iterator.next()); }
  • Entry:entry is a static internal interface in the map interface that represents a mapping of a key-value pair, such as the Key1-value1 group mapping relationship.
    • GetKey (), gets the key in this set of mappings
    • GetValue (), gets the values in this set of mappings value
    • SetValue (), modify the values in this set of mappings
    • Hashcode (), returns the hash value of this Entry
    • Equals (), compares key-value for equality

    The Map.entryset () method obtains a set of Entry that are stored in the set so that the Entry in the MAP cannot be duplicated.

    public Set<Map.Entry<K,V>> entrySet();
  • using Entry traversal
  • Set entrySet = map.entrySet();    for (Object o : entrySet) {        Map.Entry entry = (Map.Entry) o;        System.out.println(entry);      //key=value        System.out.println(entry.getKey() + " / " + entry.getValue());    }
  • *************************************************************************************************************** ***************

Java Collection Map&hashmap

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.