0

我有来自地图的值,如下所示

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 

这是我缺乏的地方。}

4

3 回答 3

2

您想创建一个MapMapS,其中外部 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);
}
于 2012-05-23T09:00:50.837 回答
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;
    }

}
于 2012-05-23T07:55:17.130 回答
2

从评论的澄清中,我假设您想过滤您的键:一张地图只包含以 开头的键1_,另一张以开头10_,等等。

使用纯 Java,您可以通过使用 aMap<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");
于 2012-05-23T08:57:03.457 回答