如何在不使用 Java Reflection API 的情况下从 HashMap 中获取对象?
假设我有一个 HashMap:
val userMap = new HashMap[String, String]
userMap += "username" -> "user"
userMap += "password" -> "pass"
和一个对象:
username:String = ""
password:String = ""
在不使用 Java Reflection API(带或不带注释)的情况下,将 HashMap 中的值放入对象的最佳方法是什么?
它可能类似于该问题: Scala - Lift - map a用于绑定的自定义装箱对象?
Let's say I have a HashMap:
val userMap = new HashMap[String, String]
userMap += "username" -> "user"
userMap += "password" -> "pass"
and an Object:
username:String = ""
password:String = ""
What would be the best way to put the values from the HashMap into the object, without using the Java Reflection API (with or without Annotations)?
It might be similar to that question: Scala - Lift - map a custom boxed object for bind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您不需要反射,因此无法自动匹配名称。但手动完成并不难。
鉴于
我会给
你一个
Option[User]
与Some
用户(如果它在该地图中),如果不在该地图中,则为None
。Since you don't want reflection, you can't match up the names automatically. But doing it manually isn't hard.
Given
I would just
which will give you an
Option[User]
withSome
user if it is in that map, andNone
if not.