当编译一小段Java代码时,我收到了一个编译注意事项,表示存在不安全的操作。我希望知道如何改变我的数据结构使其更加安全。
概念:我需要根据字符串的长度将输入的字符串组织成桶,这个长度可以是任意的(但小于80个字符)。
代码:
Map<Integer, List> buckets = new HashMap<Integer, List>();
if(!buckets.containsKey(length))
{
buckets.put(length,new Vector<wordValues>());
}
//Now add the temp to the bucket
buckets.get(length).add(new wordValues(temp));
然后我将字符串添加到相应大小的列表中。
有更好的方法吗?
containsKey
和get
? - obataku