English 中文(简体)
如何根据某些 particalar 值对地图中的值进行分组?
原标题:How to group the values from map based on some particualar values?

我有来自地图的以下数值

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

我真的正在努力将这些价值观分开,把它们放在不同的地图上。

i 愿意分组如下。

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 

this is the place where i am lacking. }

最佳回答

如果您想要将 1_ 1, 1_ 2 拆分到 1_ 19. 。 请使用字符串类的拆分() 函数 。

前。

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) + "
");
            }
        }
    }

    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;
    }

}
问题回答

从对评论的澄清来看,我假设您想过滤您的密钥:一张只包含以 < code> 1%/code > 开头的密钥的地图,另一张只有 < code> 10_/code > 等的地图。

使用普通 Java, 您可以使用 < code> Maap< String, 地图和lt; String, 字符串 & gt; & gt; 并在原始地图上迭接 :

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");

请注意,源地图或过滤后的地图后来的更改无法更新另一地图。 如果您想要类似的东西, 而不是