likedhashmap 转 对象
时间: 2023-09-21 22:12:20 浏览: 42
要将LinkedHashMap转换为对象,你可以使用ModelMapper库。以下是一个示例代码:
```java
import org.modelmapper.ModelMapper;
public class MyClass {
private int myInt;
private String myString;
public int getMyInt() {
return myInt;
}
public void setMyInt(int myInt) {
this.myInt = myInt;
}
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
}
// 创建一个LinkedHashMap
LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("myInt", 1);
linkedHashMap.put("myString", "original");
// 使用ModelMapper进行转换
ModelMapper modelMapper = new ModelMapper();
MyClass myClass = modelMapper.map(linkedHashMap, MyClass.class);
```
在上述代码中,我们首先创建了一个LinkedHashMap并填充了需要转换的数据。然后,我们使用ModelMapper实例化一个ModelMapper对象。最后,我们使用`modelMapper.map()`方法将LinkedHashMap转换为MyClass对象。
请注意,ModelMapper库可以将HashMap与实体类之间的映射关系定义在Mapper接口中,但这是可选的。在上述示例中,我们没有使用Mapper接口,而是直接在MyClass类中定义了属性和setter/getter方法。
说明了如何使用ModelMapper将HashMap转换为实体类的示例代码。你可以根据你的需求进行适当的修改和调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)