Java study notes-Pair and Map

Common point: Pair and Map are stored in key and value

difference:

  • Pair obtains the corresponding key value and value value through getKey()/getValue(), there is no operation to add key-value pairs
  • Map obtains the corresponding value through get(), obtains all values ​​through values(), and can also add new key-value pairs through put.
  • The pair saves a pair of key values, and the map can save multiple pairs of key values.

 

usage:

Pair<Integer, String> pair = new Pair<>(1, "ONE");
    pair.getKey();
    pair.getValue();

 

The test code is as follows:

public class Test {
    public static void main(String[] args) {
        Pair<Integer, String> pair = new Pair<Integer, String>(1, "One");
        Integer key = pair.getKey();
        String value = pair.getValue();
        System.out.println(key);
        System.out.println(value);


        Map<Integer, String> map=new ManagedMap<Integer, String>();
        map.put(2,"TWO");
        map.put(3,"Three");
        System.out.println(map.get(2));
        System.out.println(map.values());

    }
}

 

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/111746203