24 映射-Map

时间:2023-03-09 02:42:36
24 映射-Map

什么是映射(Map)

  映射中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复

key1 value1

key2 value2

key3 value3

key4 value4

key5 value5

import java.util.Map;

import java.util.HashMap;

public class Test

{

  public static void main(String args[])

  {

    HashMap<String,String> hashMap=new HashMap<String,String>();

    Map<String,String> map=hashMap;

    map.put("1","a");

    map.put("2","b");

    map.put("3","c");

    map.put("4","d");

    map.put("3","e");

    int i=map.size();

    System.out.println(i);

    String s=map.get("3");

    System.out.println(s);

  }

}