我有来自地图的值,如下所示
Key = 1_1, Value = 02/04/2012
Key = 1_2, Value = 03/04/2012
Key = 1_3, Value = 04/04/2012
Key = 1_4, Value = 05/04/2012
Key = 1_5, Value = 06/04/2012
Key = 1_6, Value = 09/04/2012
Key = 1_7, Value = 10/04/2012
Key = 1_8, Value = 11/04/2012
Key = 1_9, Value = 12/04/2012
Key = 1_10, Value = 13/04/2012
Key = 1_11, Value = 18/04/2012
Key = 1_12, Value = 19/04/2012
Key = 1_13, Value = 20/04/2012
Key = 1_14, Value = 23/04/2012
Key = 1_15, Value = 24/04/2012
Key = 1_16, Value = 25/04/2012
Key = 1_17, Value = 26/04/2012
Key = 1_18, Value = 27/04/2012
Key = 1_19, Value = 30/04/2012
Key = 10_20, Value = 02/04/2012
Key = 10_21, Value = 03/04/2012
Key = 10_22, Value = 04/04/2012
Key = 10_23, Value = 05/04/2012
Key = 10_24, Value = 06/04/2012
Key = 10_25, Value = 09/04/2012
Key = 10_26, Value = 10/04/2012
Key = 10_27, Value = 11/04/2012
Key = 10_28, Value = 12/04/2012
Key = 10_29, Value = 13/04/2012
Key = 10_30, Value = 16/04/2012
Key = 10_31, Value = 17/04/2012
Key = 10_32, Value = 18/04/2012
Key = 10_33, Value = 19/04/2012
Key = 10_34, Value = 23/04/2012
Key = 10_35, Value = 24/04/2012
Key = 10_36, Value = 26/04/2012
Key = 10_37, Value = 27/04/2012
我真的很难将这些值分开并将它们放在单独的地图中。
我想分组如下。
1_1到1_19这个我想基于“_”进行拆分并单独获取第一个值并将它们分组到一个单独的地图中。
像1是关键,值将是日期。
编辑:
employeeMap = showExelData(sheetData);
String previousEemployeeID = "",employeeID[];
Iterator<Map.Entry> entries = employeeMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = entries.next();
employeeID = entry.getKey().toString().split("_");
// this is the place where i want to check the values if 1 than group the values it can be even Key = 1_0, Value = 25/04/2012 to If Key = 1_18, Value = 30/04/2012
but when the other one comes ex : Key = 10_0, Value = 25/04/2012 to If Key = 10_17, Value = 30/04/2012it has to go to new Map
这是我缺乏的地方。 }
答案 0 :(得分:2)
如果要将1_1,1_2分割为1_19 ..请使用String类的split()函数。
实施例
String x = 1_19;
String[] y = x.split("_");
y [0]等于1,y [1]将为19
至于在地图中使用键的第一个值,这是不可能的,因为它需要地图中每个条目的唯一键,就像npinti在帖子上发表的那样。
public class Mapping {
Map<String, String> coMap;
List<String> coList;
public Mapping()
{
init();
}
public static void main(String[] args)
{
Mapping oMapping = new Mapping();
Map<String, Map<String, String>> oMap = oMapping.classifyMapEntries();
for ( String sParentKey : oMapping.coList )
{
Map<String, String> oChildMap = oMap.get(sParentKey);
Iterator<String> oIterator = oChildMap.keySet().iterator();
System.out.println("Map");
while( oIterator.hasNext() )
{
String sChildKey = oIterator.next();
System.out.print( "Key: " + sChildKey + ", Value: "
+ oChildMap.get(sChildKey) + "\n");
}
}
}
private void init()
{
coMap = new HashMap<String, String>();
coList = new ArrayList<String>();
coMap.put("1_1", "a");
coMap.put("1_19", "a");
coMap.put("10_1", "b");
coMap.put("10_19", "b");
}
private Map<String, Map<String, String>> classifyMapEntries()
{
Map<String, Map<String, String>> oClassified =
new HashMap<String, Map<String,String>>();
Iterator<String> oIterator = coMap.keySet().iterator();
while( oIterator.hasNext() )
{
String sKey = oIterator.next();
String sFirst = sKey.substring(0,sKey.indexOf("_"));
if ( !coList.contains(sFirst) )
{
coList.add(sFirst);
}
}
for ( String sKey : coList )
{
Map<String, String> oChildMap = new HashMap<String, String>();
Iterator<String> oIterator2 = coMap.keySet().iterator();
while( oIterator2.hasNext() )
{
String sChildKey = oIterator2.next();
String sParentKey = sChildKey.substring(0,sChildKey.indexOf("_"));
if ( sKey.equals(sParentKey) )
{
oChildMap.put(sChildKey, coMap.get(sChildKey));
}
}
oClassified.put(sKey, oChildMap);
}
return oClassified;
}
}
答案 1 :(得分:2)
根据评论的说明,我假设您希望过滤您的密钥:一个地图仅包含以1_
开头的密钥,另一个地图仅包含10_
等。
使用普通Java,您可以使用Map<String, Map<String, String>>
并迭代原始地图来执行此操作:
Map<String, Map<String, String>> filtered = new HashMap<String, Map<String, String>>();
for (Entry<String, String> sourceEntry : source.entrySet()) {
String keyPart = sourceEntry.getKey().split("_")[0];
Map<String, String> filteredTarget = filtered.get(keyPart);
if (filteredTarget == null) {
filteredTarget = new HashMap<String, String>();
filtered.put(keyPart, filteredTarget);
}
filteredTarget.put(sourceEntry.getKey(), sourceEntry.getValue());
}
Map<String, String> oneMap = filtered.get("1");
assert oneMap.get("1_19").equals("30/04/2012");
assert filtered.get("10").get("10_37").equals("27/04/2012");
请注意,源地图或过滤地图中的后续更改不会更新其他地图。如果你想拥有类似的东西,那么 Guava 可以提供帮助:
Map<String, Map<String, String>> filtered = Maps.newHashMap();
for (Entry<String, String> sourceEntry : source.entrySet()) {
final String keyPart = sourceEntry.getKey().split("_")[0];
Map<String, String> filteredTarget = filtered.get(keyPart);
if (filteredTarget == null) {
filteredTarget = Maps.filterKeys(source, new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith(keyPart + "_");
}
});
filtered.put(keyPart, filteredTarget);
}
}
Map<String, String> oneMap = filtered.get("1");
assert oneMap.get("1_19").equals("30/04/2012");
assert filtered.get("10").get("10_37").equals("27/04/2012");
oneMap.put("1_50", "Test");
assert source.get("1_50").equals("Test");
答案 2 :(得分:2)
您想要创建一个Map
的MapS,其中外部Map的键是给定Map的第一个键数。以下代码未经过测试,但您可以将其作为提示......
Map<String, Date> givenMap; // this is the given Map
Map<Integer, Map<Integer, Date> > newMap = new HashMap<Integer, Map<Integer, Date> > ();
for (Map.Entry<String, Date> entry : givenMap.entrySet()) {
String givenKey = entry.getKey();
Date givenDate = entry.getValue();
String[] splittedKey = givenKey.split("_");
int newOuterKey = Integer.parseInt(splittedKey[0]);
int newInnerKey = Integer.parseInt(splittedKey[1]);
if (!newMap.containsKey(newOuterKey)) {
newMap.put(newOuterKey, new HashMap<Integer, Date> ();
}
newMap.get(newOuterKey).put(newInnerKey, givenDate);
}