9th collection in Java (MAP)

Source: Internet
Author: User
Tags object object set set

Definition of Map interface map interface in Java: public interface map<k,v>

It is obvious that this is a generic interface and accepts two parameters: K, v. K represents key,v for value. Map stores a series of key-value pairs, each of which is mapped to a value.

Some points to note in the map interface:
    • A key can only be mapped to a value
    • Cannot contain duplicate keys
    • The structure of the map is unordered and cannot be accessed by subscript

The map interface provides three collection views that allow you to view the contents of a map in the form of a keyset (KeySet), a value set (values), or a key-value mapping relationship set (EntrySet).

Keyset View:set<k> KeySet()

The method returns a set set of type K for the map key. This set is supported by mappings, which means that when we change the set keyset, the original HashMap is also subject to the same changes, which is a very scary point.

When we want to traverse the HashMap, this method can be used to get the HashMap key collection, through the iterator to traverse the key to get the value stored in the map.

// get a keyset and traverse get values Set keysets = hashmap.keyset ();  for (Object key:keysets) {    hashmap.get (key);}
get a keyset and traverse get valuesValues View:collection<v> Values()

Returns a view of the values contained in this map Collection . The collection is supported by the map, which is the same as the above set, and the HashMap is affected when we change a value. You can use this method when you only need to know the value and ignore the traversal of the key. The traversal method also uses iterators (to strengthen the For loop).

EntrySet View:set<map.entry<k,v>> EntrySet()

This is the most important view in map, which returns a set set of key-value pairs, meaning that each element in the set is a key-value pair in a map. The set is supported by mappings.

EntrySet returns a Set<map.entry<k,v>> collection, The element type in the collection is the Map.entry type, which is an internal interface in the map interface, where the implementation class for the map, such as HashMap, has its implementation class, which represents a node in the map.

Methods in EntrySet:

    • getKey() 返回与此项对应的键。
    • getValue() 返回与此项对应的值。
    • setValue(V value) 用指定的值替换与此项对应的值。
Some other common methods of map:
    • containsKey(Object key) 判断Map中是否包含该键
    • containsValue(Object value) 判断Map中是否存在该value
    • get(Object key) 通过键来获取其在Map中映射的值
    • put(K key, V value) 添加键值对,如果该键已存在的话,将把原有值覆盖
    • putAll(Map<? extends K,? extends V> m) 添加Map对象中的所有键值对
    • remove(Object key) 通过键移除映射关系
    • size() 获取Map中存在的键值对数量
The common implementation class of MAP is HashMap: The structure is a hash table, its data is unordered, and the elements are compared by the hash value and equals. TREEMAP: The underlying structure is a two-fork tree, and the elements are sorted when stored. For an object that does not implement a comparison algorithm, it is necessary to pass in an comparator comparer for that object when constructing the TreeMap or to have the object implement the comparable interface. The exact implementation is the same as TreeSet. Thinking: How to save the number of occurrences of each letter in a string to a dictionary and find the first letter that appears two times

I use HashMap here, first we want to add the number of letters to map, the letter as the key, the number of occurrences as a value:

New HashMap ();          for (char  c:str.tochararray ()) {    // If the letter already exists in the map, add the number of times (value) to it, otherwise the key will be added with a value of 1    if (Hmc.containskey (c)) {                        int count = hmc.get (c);         + +count)    ; Else {        1);}                    }

Next we need to find the key value pair with a value of 2, get the key (letter) of the key-value pair in the string where it first appears (INDEXOF), and store it through an intermediate variable. Then we get the first occurrence of the smallest key in the form of Daleitai, which is the letter we're looking for:

Set set = Hmc.entryset ();//get a set of key-value pairsintindex =-1;//the position of the letter to be found for(Object object:set) {Entry node= (Entry) object;//The elements of the set of key-value pairs, that is, the entry node    CharKey = (Char) Node.getkey ();//get Key (letter)    if((int) Node.getvalue () ==2) {        intnum = Str.indexof (key);//the position of the first occurrence of the key (letter) in the string        if(Index==-1 | | str.indexof (KEY) <index) {//Daleitai, who's small to take whoindex =Str.indexof (key); }}}

9th collection in Java (MAP)

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.