在地图列表中查找键的值
我有 maps list< map< string,string>>的 list 输入
。
可以按以下方式表示:
[{AddressField=AddressUsageType, AddressValue=PRINCIPAL},
{AddressField=StreetNumber, AddressValue=2020},
{AddressField=StreetName, AddressValue=Some street}]
我想获得特定address> addressfield
的addressValue
。
例如,我想获得 value “ principal”
key “ adverseUsageType”
。
我尝试使用过滤器和许多其他地图功能,但最终无法获得适当的解决方案。
这是我的代码片段,可获得第一键值对的值:
DataTable table;
List<Map<String,String>> input= table.asMaps(String.class, String.class);
String AddressField = input.get(0).get("AddressField");
String AddressValue = input.get(0).get("AddressValue");
System.out.println("AddressField " +AddressField);
System.out.println("AddressValue " +AddressValue);
这是上述片段的输出:
AddressField AddressUsageType
AddressValue PRINCIPAL
I have a list of maps List<Map<String,String>> input
.
Which can be represented in the following manner:
[{AddressField=AddressUsageType, AddressValue=PRINCIPAL},
{AddressField=StreetNumber, AddressValue=2020},
{AddressField=StreetName, AddressValue=Some street}]
I would like to get the AddressValue
for a particular AddressField
.
For example, I want to get the value "PRINCIPAL"
for the key "AddressUsageType"
.
I have tried using filters and many other MAP functions, but couldn't end up with a proper solution.
This is my code snippet that gets the value of 1st key-value pair:
DataTable table;
List<Map<String,String>> input= table.asMaps(String.class, String.class);
String AddressField = input.get(0).get("AddressField");
String AddressValue = input.get(0).get("AddressValue");
System.out.println("AddressField " +AddressField);
System.out.println("AddressValue " +AddressValue);
Here is the output of the above snippet:
AddressField AddressUsageType
AddressValue PRINCIPAL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于在您的代码中,您有一个
list
,其中每个元素都是MAP
带有单个映射,您可以流式传输列表,并过滤包含值的第一个映射地址useageType
。您的代码可以像这样写:
这也是 onecompiler 的测试主。
Since in your code you have a
List
where each element is aMap
with a single mapping, you could stream the list and filter for the first map containing the valueAddressUsageType
.Your code could be written like this:
Here is also a test main at OneCompiler.
您正在滥用
MAP
此处。因为预计每张地图都有有限,定义明确的键,每个键在您的域中具有特殊的含义。每个地图都是指代表某种地址的意思。强烈建议将这些数据在域模型中具有特定含义后立即将这些数据分组为自定义对象。
优点是:
String
。我知道它可能是其他人很久以前开发的代码,或者您可能认为现在存储数据更容易。但是,您推迟重构的越多,它就越昂贵。
通过将对象视为集合,您可以使用深度嵌套的集合,例如地图的地图或将影响代码可维护性的地图列表的地图。
如果您选择将数据分组为域类,则可以处理此任务。
为了简单性和简洁性,我将使用Java 16记录。
advellyUsageType
将表示为enum
。如果我正确理解的地址类型数量有限,那么以 enum 而不是依靠字符串值而存储此数据的数量有限,并且提供了额外的机会。这就是您可以通过能够从其类型中受益而不是将其视为字符串的每个
userAddress
对象从每个userAddress
对象提取特定字段的方式 : /em>地址和函数从地址对象提取特定属性。
main()
- 演示输出
You are misusing the
Map
here. Because every map is expected to have a limited, well-defined set of keys, each of which has a particular meaning in your domain. And every map is mean to represent a some kind of address.It's highly advisable to group these data into a custom object as soon as it has a particular meaning in your domain model.
The advantages are:
String
.I understand that it might the code that was developed long ago by someone else, or you might think that for now it's easier to store the data this was. But the more you're postponing the refactoring, the more expensive it becomes.
By treating the object as a collection, you might and up with deeply nested collections like map of maps or map of list of maps that will affect maintainability of code.
That how you can approach this task if you would choose to group the data into a domain class.
For sake of simplicity and conciseness, I would use Java 16 records.
And
AddressUsageType
will be represented asenum
. If I understood correctly there will a limited number of address type, so it's less error-prone to store this data as enum instead of relying on string values, and also gives few extra opportunities.And that's how you can extract a particular field from each
UserAddress
object in a list, by being able to benefit from its type instead of treating them as string:The method shown above expects a list of addresses and a function which extracts a particular property from an addresses object.
main()
- demoOutput
您对地图的使用有些奇怪,因为您的实际键“ adverseUsageType”是地图内的一个值,每个地图只是一对键,值存储在静态键后面。如果您不能更改它,则可以使用类似的事情:
Your usage of Maps is a bit odd here because your actual key "AddressUsageType" is a value inside the map and every map is just one pair of key and value stored behind static keys. If you can not change that, you could go with something like this:
过滤输入以仅保留包含
addressField = addressUsageType
的映射。然后使用.map
函数提取addressValue
输入,然后将它们收集到结果列表中。输出是
Filter the input to retain only maps which contain
AddressField=AddressUsageType
. Then extract theAddressValue
entry using the.map
function and gather them in a result list.The output is