2012-01-03 101 views
0

我想结合一些指标与他们对应的字符串HashMap的关键,所以我试图用一个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; 
} 
} 

但我没有任何与它的运气。我想,“如果”让每一个和该字符串(文本)“=”,并在一起映射,但似乎我做错了什么,因为我的输出是这样的:

{如果= [ 0]}

{如果= [0],== [8]}

{如果= [0],== [9]}

{如果= [0],== [012]

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

{如果= [26],== [23]}

{如果= [26],== [36]}

我寻找类似:

如果= [0,26]和= = [8,9,15,23,23,36]

我需要一些帮助来弄清楚我在这里犯了什么错误?或者我只是没有把它们正确地打印出来?任何帮助将不胜感激。

谢谢

丹尼尔

回答

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

谢谢你做到了。谢谢大家的帮助,我很高兴我能问这些简单的问题,而且人们花时间回答。 – DVM 2012-01-03 14:14:58

+1

@DanielV:虽然这确实解决了问题,但我仍然建议您使用番石榴。你不喜欢你的代码是简单的:'sign.put(a,index);'?这就是Multimap会给你的。 – 2012-01-03 14:32:53

+0

哦,我会,但我被要求至少现在不要使用任何“外部工具”(为什么我不知道)。 – DVM 2012-01-03 14:43:47

3

听起来像是你想有一个multimap - 我建议你使用Guava及其Multimap接口的实现,所以你不必重新发明轮子:)

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

  • 检查是否a已经存在在地图上
    • 如果是这样的一个关键,获取价值(现有列表),并添加index
    • 如果没有,创建一个新的列表,把在地图上,并添加index
+0

我不认为他想多地图,他想用一个键和多个值的HashMap中。问题是他没有重复使用列表并在循环中重新创建它。 – Manoj 2012-01-03 14:00:25

+0

谢谢,我目前正在寻找谷歌MultiMap。但有没有办法做到这一点,而不使用这些? – DVM 2012-01-03 14:00:48

+0

@Manoj:一个具有多个值的键听起来像是一个multimap对我来说...... – 2012-01-03 14:02:47

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

请注意,当您更新现有列表时,不需要调用'signs.put';你可以把它放在'if'语句中。 – 2012-01-03 14:22:41

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