map

使用

  • 使用 Map<KeyType, ValueType> 声明引用变量
  • 使用实现类(如:new HashMap<>()),创建被引用对象

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
      @RestController
      public class ProductServiceController {
    private static Map<String, Product> productRepo = new HashMap<>();
    static {
     Product honey = new Product();
     honey.setId("1");
     honey.setName("Honey");
     productRepo.put(honey.getId(), honey);
     
     Product almond = new Product();
     almond.setId("2");
     almond.setName("Almond");
     productRepo.put(almond.getId(), almond);
    }
      }

实现类

  • HashMap