设置<string> getSet(String s)</string>

时间:2014-11-16 04:49:20

标签: java string set

我想从我创建的Hashmap返回一个集合,并且应该位于hashmap中,但无论出于什么原因它都不能正常工作。这是我的方法:

public Set<String> getSet(String s){
    // returns the Set<String> that is associated with the String s
    String stand = standardize(s);

    if(check(stand)){

        System.out.println(map.get(stand));

    }

    return null;

}

我们还有一个列表,但我将其转换为地图。标准化将字符串更改为按字母顺序排列,从而将字符串排除在外。该字符串用作键。我试图打印出那套,但无论出于何种原因,它都不起作用。我究竟做错了什么?附:这段代码是用Java编写的。

2 个答案:

答案 0 :(得分:0)

你试过吗

return map.get(stand);

答案 1 :(得分:0)

没有standardize(s)check(s)的详细信息,很难猜到。如果顾名思义check(s),请检查地图是否包含s作为关键字,那么问题很可能来自standadize(s)。这是一个如何顺利运作的例子:

static HashMap<String,Set<String>> map=new HashMap<String, Set<String>>();
public static void main(String[] args) {

  TreeSet<String> set1=new TreeSet<String>();
  set1.add("Zoralikecury");
  set1.add("Xline");
  set1.add("XYZ");
  map.put("1", set1);
  map.put("2", null);
    System.out.println(getSet("1"));

}

public static Set<String> getSet(String s){
    if(check(s))
        return map.get(s);
  return null;
}
public static boolean check(String s)
{
   Iterator<String>it= map.keySet().iterator();
   while(it.hasNext())
   if(it.next().equals(s))
       return true;
   return false;
}
相关问题