gpt4 book ai didi

java - 如何将其解析为多值映射?

转载 作者:行者123 更新时间:2023-12-01 15:06:12 26 4
gpt4 key购买 nike

我有一个字符串名称,5486984,189.658.235.215,3.158.68.15,name2,189189,189.658.218.268,3.158.68.13...

等等

我想将其放入 map 中,但我不完全确定。我正在查看具有多值映射的 Apache Commons Collection 库。即使我使用它,我也不确定如何解析这个 StringArray 。

编辑:

感谢您的回复。我的总体目标是让该字符串显示在 JSF 下拉列表中。我有

<f:selectItems itemValue="#{mainBean.information}" value="#mainBean.information}" />

在我的Java中我有

Private String [] information;
information = config.getStringArray("information"); // This brings in this string from the property file

我一直在引用这个 http://www.horstmann.com/corejsf/refcard.html

因此,我刚刚发布了这里的内容。它将填充该字符串数组的每个元素。我只希望显示“名称”,并将其他三个值作为实际“值”传递。

我想我可能需要提出一个新问题。

我尝试创建一个由字符串和字符串数组组成的 Map 对象,但 JSF 拒绝接受它。

这是新问题: JSF I need to populate selectItems with a StringArray but I need to parse it first

最佳答案

由于您使用 Apache Commons 进行多重映射,并且您知道有四个值:

MultiMap map = new MultiHashMap();    
String[] elements = source.split(",");

// process the elements in groups of four
for (int keyIndex = 0; keyIndex < elements.length; keyIndex += 4) {
String key = elements[keyIndex];
map.put(key, elements[keyIndex+1]);
map.put(key, elements[keyIndex+2]);
map.put(key, elements[keyIndex+3]);
}

最后,您会得到一个 MultiMap,每个键包含三个元素。您当然应该对数据进行一些健全性检查,但基本思想如上所述。

关于java - 如何将其解析为多值映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com