本帖最后由 tianranhe 于 2013-05-21 08:09:17 编辑

解决方案 »

  1.   

    Map<String,Map<String,Double>>
      

  2.   


    我试过HashMap<String, HashMap<String,Double>>不行,要用Map??
      

  3.   


    我试过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再调试下吧~
      

  4.   


    我试过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()函数没有找到二层哈希图
      

  5.   

    问题找到了:word截取错误。改为:
    String word = line.substring(line.indexof('\t'), line.lastIndexof('\t')).trim();
      

  6.   


    我试过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));//////
                }