具有多个值的HashMap键

时间:2012-01-03 13:48:42

标签: java

我正在尝试绑定一些索引,它们是相应的字符串,所以我尝试使用HashMap,如下所示:

public class NewClass {
static List<String> referenceList = new ArrayList<String>();

public static void main(String[] args) {

    String x = "AGE_X";
    String y = "AGE_Y";
    //String w = "AGE_Z";
    referenceList.add(x);
    referenceList.add(y);

    String text1 = "if ( AGE_X = = 10 ){\r\n"
            + " j = 1;"
            + " AGE_X = 5 ;"
            + " if ( true ) {"
            + " m = 4 / AGE_Y ;"
            + " }"
            + " }";
    detectEquals(text1);
}

public static String detectEquals(String text) {
    String a = null;
    // text = TestSplitting.addDelimiters(text);
    String[] newString = text.split(" ");
    List<String> test = Arrays.asList(newString);
    StringBuilder strBuilder = new StringBuilder();
    HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>();
    List<Integer> refList = new ArrayList<Integer>();
    int index = 0;

    for (int i = 0; i < test.size(); i++) {
        a = test.get(i).trim();
        //System.out.println("a=" + a);
        strBuilder.append(a);   
        index = strBuilder.length() - a.length();

        if (a.equals("if") || a.equals("=")) {         
            refList.add(index);
            // System.out.println(refList);
            signs.put(a, refList);
            refList = new ArrayList<Integer>();

            System.out.println(signs);
        }
    }
    return a;
}
}

但我没有运气。我试图从该字符串(text1)获取每个“if”和“=”并将它们映射到一起,但似乎我做错了,因为我的输出看起来像这样:

  

{如果= [0]}

     

{if = [0],== [8]}

     

{if = [0],== [9]}

     

{if = [0],== [15]}

     

{if = [0],== [23]}

     

{if = [26],== [23]}

     

{if = [26],== [36]}

我正在寻找类似的东西:

  

if = [0,26] and = = [8,9,15,23,23,36]

我需要一些帮助来弄清楚我在哪里弄错了?或者我只是不打印出来?任何帮助将不胜感激。

谢谢,

丹尼尔

4 个答案:

答案 0 :(得分:3)

听起来你想要一个multimap - 我建议你使用Guava及其Multimap接口的实现,这样你就不必重新发明轮子了。)

我不太清楚你要做什么,但是你的现有代码总是在在地图中引用旧列表之后创建一个新列表 ...我不知道我知道你为什么这样做。我期待更多的东西:

  • 检查a是否已作为地图中的关键字存在
    • 如果是,请获取值(现有列表)并向其添加index
    • 如果没有,请创建一个新列表,将其放入地图中,然后将index添加到其中

答案 1 :(得分:0)

像这样更改你的代码

public static String detectEquals(String text) {
            String a = null;
            // text = TestSplitting.addDelimiters(text);
            String[] newString = text.split(" ");
            List<String> test = Arrays.asList(newString);
            StringBuilder strBuilder = new StringBuilder();
            HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>();
            List<Integer> refList = new ArrayList<Integer>();
            int index = 0;

            for (int i = 0; i < test.size(); i++) {
                a = test.get(i).trim();
                //System.out.println("a=" + a);
                strBuilder.append(a);
                index = strBuilder.length() - a.length();

                if (a.equals("if") || a.equals("=")) {
                    // System.out.println(refList);
                    refList = signs.get(a);
                    if(refList == null) {
                        refList = new ArrayList<Integer>();
                    }
                    refList.add(index);

                    signs.put(a, refList);

                }
            }
            System.out.println(signs);
            return a;
        }

答案 2 :(得分:0)

试试这个

 if (a.equals("if") || a.equals("=")) {         

            // System.out.println(refList);
            if(signs.containsKey(a)){
                signs.get(a).add(index);
            }
            else{
                refList.add(index);
                signs.put(a, refList);
            }
            refList = new ArrayList<Integer>();

            System.out.println(signs);
        }

答案 3 :(得分:0)

public static void main(String[] args)
{
    String x = "AGE_X";
    String y = "AGE_Y";
    referenceList.add(x);
    referenceList.add(y);
    String text1 = "if ( AGE_X = = 10 ){\r\n" + " j = 1;" + " AGE_X = 5 ;" + " if ( true ) {" + " m = 4 / AGE_Y ;"
            + " }" + " }";
    detectEquals(text1);
}

public static String detectEquals(String text)
{
    String a = null;
    String[] newString = text.split(" ");
    List<Integer> tests = new ArrayList<Integer>();
    List<Integer> equals = new ArrayList<Integer>();
    int index = 0;
    for(int i = 0; i < newString.length; i++)
    {
        a = newString[i].trim();
        //System.out.println("a=" + a);
        if(a.equals("if"))
        {
            tests.add(index);
        }
        else
        {
            if(a.equals("="))
            {
                equals.add(index);
            }
        }
        index += newString[i].length();
    }
    print("if", tests);
    print("=", equals);
    return a;
}

private static void print(String s, List l)
{
    System.out.print(s+"=[");
    Iterator i = l.iterator();
    if(i.hasNext()) System.out.print(i.next());
    while(i.hasNext())
        System.out.print(","+i.next());
    System.out.println("]");
}