HashMap Extension-Dict

Origin

If you are familiar with Python, you must know that it has a data structure called dict, which is also a KV (Key-Value) structured data structure similar to Map in Java, but provides more flexible and diverse usage. The Dict object in Hutool aims to implement a more flexible KV structure, providing rich getXXX operations for strong types and extending HashMap into a data structure without type distinction.

Introduction

Dict inherits HashMap, with its key being of type String and value being of type Object. It provides get methods for different types by implementing the BasicTypeGetter interface, and provides conversion methods for Beans, greatly enhancing the flexibility of Map.

Entity in Hutool-db is a subclass of Dict and serves as a medium for data.

Usage

Creation

Dict dict = Dict.create()
 .set("key1", 1)//int
 .set("key2", 1000L)//long
 .set("key3", DateTime.now());//Date

Create a Dict object through chained construction and use it like a Map.

Getting values of a specific type

Long v2 = dict.getLong("key2");//1000