如何将其解析为多值映射? [英] How do I parse this into a MultiValued Map?

查看:109
本文介绍了如何将其解析为多值映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以此类推

我当时想将其放入地图中,但我不确定.我正在查看具有多值映射的Apache Commons Collection库.即使我使用它,我也不确定如何将这个StringArray解析为它.

I was thinking that I would put this into a map but I'm not entirely sure. I'm looking at the Apache Commons Collection library that has Multivalued map. Even if I use that I am unsure how to parse this StringArray into it.

感谢您的回复.我的总体目标是使String出现在JSF下拉列表中. 我有

Thank you for the response. My overall goal is getting that String to show up in a JSF Drop down. I have

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

在我的Java中,我有

in my Java I have

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

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

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

So with what I have here, that I just posted. It will populate every element of that string array. I only want the "name" to show up and the three other values to be passed as the actual "value".

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

I may need to make a new question I guess.

我试图制作一个Map对象,该对象是一个字符串和一个字符串数组,但是JSF拒绝使用它.

I tried to make a Map object that is a string and a string array but JSF refuses to take it.

这是新问题: JSF我需要用StringArray填充selectItems,但我需要先对其进行解析

推荐答案

由于您将Apache Commons用于多图,因此您知道有四个值:

Since you are using Apache Commons for the multimap, and you know there are four values:

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,每个键包含三个元素.您当然应该对数据进行一些完整性检查,但基本思想已在上面.

In the end you get a MultiMap with three elements per key. You should of course do some sanity checks on the data, but the basic idea is above.

这篇关于如何将其解析为多值映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆