解决方案 »
- 请教:java程序中super()的使用问题
- 如何判断java流的末尾????
- 多线程的问题,发生死锁
- 一个最简单的java程序,没有任何import 为什么还能System.out.println?谢谢
- Java 怎样设置文件的属性(随便一个文件的“只读”“隐藏”“存档”)
- 【急急急】JNI调用C++dll获取硬盘信息,【只有】在命令提示符下调用才成功返回硬盘信息(返回自定义常量没有问题)
- 如何设置JScrollPane中的滚动条的宽度(大小)?
- 一个关于JSP调用JSwing问题!
- JComboBox如何只点击下拉按钮才下拉?!
- java类型转换
- 在字符串中找出连续最长的数字串,必须是连续的
- hadoop系统可以部署在windows操作系统上吗
我试过HashMap<String, HashMap<String,Double>>不行,要用Map??
我试过HashMap<String, HashMap<String,Double>>不行,要用Map??
因为你把第二层的HashMap<String,Double>封装成TagNode类了,其实没必要。有点说不清,稍微调整点代码你试试吧。
static HashMap<String, HashMap<String,Double>> TagSet = new HashMap<String, HashMap<String,Double>>();
static void readBi(){
try {
FileReader fr = new FileReader("data/Bi");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
HashMap<String,Double> temp;
while((line=bufr.readLine()) !=null ){
String tag = line.substring(0, line.indexOf('\t'));
String word = line.substring(line.indexOf('\t')+1, line.indexOf('\t')+1);
String freqStr = line.substring(line.lastIndexOf('\t')+1);
double freq = Double.parseDouble(freqStr);
temp = TagSet.get(tag);
if(temp == null){
temp = new HashMap<String,Double>();
}
temp.put(word, freq);
TagSet.put(tag, temp);
System.out.println(temp.get(word));//////
}
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
那个对temp的操作,以及TagSet.put(tag, temp);比较关键。其它代码LZ再调试下吧~
我试过HashMap<String, HashMap<String,Double>>不行,要用Map??
因为你把第二层的HashMap<String,Double>封装成TagNode类了,其实没必要。有点说不清,稍微调整点代码你试试吧。
static HashMap<String, HashMap<String,Double>> TagSet = new HashMap<String, HashMap<String,Double>>();
static void readBi(){
try {
FileReader fr = new FileReader("data/Bi");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
HashMap<String,Double> temp;
while((line=bufr.readLine()) !=null ){
String tag = line.substring(0, line.indexOf('\t'));
String word = line.substring(line.indexOf('\t')+1, line.indexOf('\t')+1);
String freqStr = line.substring(line.lastIndexOf('\t')+1);
double freq = Double.parseDouble(freqStr);
temp = TagSet.get(tag);
if(temp == null){
temp = new HashMap<String,Double>();
}
temp.put(word, freq);
TagSet.put(tag, temp);
System.out.println(temp.get(word));//////
}
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
那个对temp的操作,以及TagSet.put(tag, temp);比较关键。其它代码LZ再调试下吧~
看半天也不容易,谢谢。
问题的关键不在这儿,我在getBi()函数中添加打印二层HashMap关联数的语句:
System.out.println(TagSet.get(tag).size());
结果返回值是1,这个值本应该是500以上的。可见
①二层哈希图没有存储成功,或者
②getBi()函数没有找到二层哈希图
String word = line.substring(line.indexof('\t'), line.lastIndexof('\t')).trim();
我试过HashMap<String, HashMap<String,Double>>不行,要用Map??
因为你把第二层的HashMap<String,Double>封装成TagNode类了,其实没必要。有点说不清,稍微调整点代码你试试吧。
static HashMap<String, HashMap<String,Double>> TagSet = new HashMap<String, HashMap<String,Double>>();
static void readBi(){
try {
FileReader fr = new FileReader("data/Bi");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
HashMap<String,Double> temp;
while((line=bufr.readLine()) !=null ){
String tag = line.substring(0, line.indexOf('\t'));
String word = line.substring(line.indexOf('\t')+1, line.indexOf('\t')+1);
String freqStr = line.substring(line.lastIndexOf('\t')+1);
double freq = Double.parseDouble(freqStr);
temp = TagSet.get(tag);
if(temp == null){
temp = new HashMap<String,Double>();
}
temp.put(word, freq);
TagSet.put(tag, temp);
System.out.println(temp.get(word));//////
}
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
那个对temp的操作,以及TagSet.put(tag, temp);比较关键。其它代码LZ再调试下吧~
看半天也不容易,谢谢。
问题的关键不在这儿,我在getBi()函数中添加打印二层HashMap关联数的语句:
System.out.println(TagSet.get(tag).size());
结果返回值是1,这个值本应该是500以上的。可见
①二层哈希图没有存储成功,或者
②getBi()函数没有找到二层哈希图空白字符惹的祸,做如下修改:
while ((line = bufr.readLine()) != null)
{
String[] lables = line.split("\\s");
//String tag = line.substring(0, line.indexOf('\t'));
String tag = lables[0];
//String word = line.substring(line.indexOf('\t') + 1, line.indexOf('\t') + 1);
String word = lables[1];
//String freqStr = line.substring(line.lastIndexOf('\t') + 1);
String freqStr = lables[2];
double freq = Double.parseDouble(freqStr); temp = TagSet.get(tag);
if (temp == null)
{
temp = new HashMap<String, Double>();
}
temp.put(word, freq);
TagSet.put(tag, temp);
System.out.println(temp.get(word));//////
}