gpt4 book ai didi

java - 如何根据某些特定值对 map 中的值进行分组?

转载 作者:行者123 更新时间:2023-12-01 16:34:36 25 4
gpt4 key购买 nike

我有来自 map 的值,如下所示

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

我真的很难将这些值分开并将它们放入单独的 map 中。

我想按如下方式分组。

1_1 到 1_19 我想根据“_”进行分割并单独获取第一个值并将它们分组到一个单独的 map 中。

例如 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

这就是我欠缺的地方。 }

最佳答案

如果要将 1_1、1_2 拆分为 1_19..,请使用 String 类的 split() 函数。

例如。

String x = 1_19;

String[] y = x.split("_");

y[0] 等于 1,y[1] 等于 19

至于使用 map 中键的第一个值,这是不可能的,因为它需要 map 中的每个条目都有一个唯一的键,就像 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;
}

}

关于java - 如何根据某些特定值对 map 中的值进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10715207/

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