Java HashMap迭代未显示键和变量

时间:2016-02-28 06:11:40

标签: java hashmap iterator

我正在尝试迭代HashMap,当检测到重复键(key = node coord)时,我想给新节点提供相同的key(coord)相同的nodeID(Hasmap值)。这是我的问题代码块和我的输出。正如您所看到的那样,输出键(键)和新键(finishedLine3)具有相同的值,但它不会进入"如果"声明并更改nodeID。

历史:我正在和#34; =="这里但也试过if(keys.equal(finishedLine3))并获得相同的结果。

我也在输出中显示 节点键:-86.88642133.035899节点值:00000001 和 节点键:-86.88642133.035899节点值:00000010 是相同但在迭代期间我的逻辑不允许这两个确切键的比较进入我的"如果"块。

这是我的代码:

    package com.KatsProject.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class MakeFile {
    public static void main(String[] args) throws IOException {
         DecimalFormat df = new DecimalFormat("00000000");
         Integer numWords = 0;
         Integer numLines = 1;
         Integer numNodes =0;
         Integer nodeID =0;
         Integer counter=0;
         String nodesID2 ="";
         String newNodeID="";
         String  header = "road_id      #_of_nodes          node_id         node_x_coord        node_y_coord    ... end of list";
         String finishedLine1 = " ";
         String finishedLine2 = " ";
         String finishedLine3 = " ";
         String finishedLine34 = " ";
         String finishedLine = " ";
         String roadID= "";
         String roadID2= "";
         String match=" ";
         String match2=" ";
         String temp=" ";
         BufferedReader br= null;
         BufferedWriter bw= null;
         HashMap<String, String> nodes = new HashMap<String, String>();
         br = new BufferedReader(new FileReader("C:\\Users\\gary\\Desktop\\roads.txt"));
         try {
                bw = new BufferedWriter(new FileWriter("C:\\Users\\gary\\Desktop\\Secretfile"));
            } catch (IOException e) {
                System.out.println("Error writing file");
                e.printStackTrace();
            }
         String line = null;
         while( (line = br.readLine())!= null )
         {   
            if(numLines > 1){
            numWords =0;  
            nodeID +=1; 
            newNodeID= df.format(nodeID);
            nodesID2 = newNodeID;
            String [] tokens = line.split("\\s+");
            roadID = tokens[0]; // save roadID for comparison
            finishedLine1="";
              for(String s :tokens){
                  numWords+=1; 
                  if(numWords == 1){
                     // x.setRoadID(roadID);
                  }  
                  if(numWords ==2 ){
                     match += s;
                  }
                  if(numWords ==3 ){
                      match2 += s;
                      //System.out.println(match2+"this is match2 ,  " + match + "  this is match");
                  }
                  if(numWords == 4 ){
                      finishedLine3 += s;               //for hash table
                      finishedLine3.trim();
                      finishedLine1 += s;
                      finishedLine1 += "      ";
                      //System.out.println(x.getX_coord()+ "   this is x_coord.");
                  }
                  if(numWords == 5){
                      finishedLine3 += s;                 //for hash table
                      finishedLine3.trim();
                      finishedLine1 += s;
                      finishedLine1 += "      ";
                      String key =finishedLine3;
                      //System.out.println("It is    " + nodes.containsKey(key)+ "  that the nodeID for the key :    "+ finishedLine3 + "    is:     " + nodes.get(key)+ "      "+ key );
                      //System.out.println(finishedLine3+ " Is this the line that also includes the roadID #?");
                      //nodes.put(key,newNodeID );
                      Set<String> keySet = nodes.keySet();
                      Iterator<String> keyIterator = keySet.iterator();
                      //System.out.println("Another iteration starts , this is iteration :       "+ nodeID);
                      bw.write("Another iteration starts , this is iteration :       "+ nodeID);
                      bw.write("\n");
                      while (keyIterator.hasNext()) {
                          String keys = keyIterator.next();
                          //System.out.println( keys);
                          //System.out.println(" nodes key: " + keys + " nodes value: " + nodes.get(keys));
                          //System.out.println("Is keys:  "+keys + " equal to finishedLine3 :  " +finishedLine3);
                          if (keys.equals(finishedLine3))
                          {

                              //System.out.println( "This is finishedLine3 :  "+finishedLine3 +" , and it is equal to:  " + keys);  
                              newNodeID = nodes.get(keys);
                              // System.out.println(nodes.get(key) + "     this is nodes.get(key) ");
                              //System.out.println(nodes.get(keys) + "     this is nodes.get(keys) ");
                              //finishedLine2+=nodes.get(finishedLine3);
                              finishedLine3="";
                              }
                      }
                      nodes.put(key,newNodeID );
                      match2="";
                      match="";

                   }
              }
              if ((!(roadID.equals(roadID2)))&&(match.equals(match2)))
              {
                   finishedLine+= roadID;
                   finishedLine+= "              ";
                   finishedLine+=numNodes;
                   finishedLine+= "             ";
                   finishedLine+=finishedLine2;
                   finishedLine+="...end of list \n";
                   bw.write(finishedLine);
                   bw.write("\n");
                   finishedLine=" ";
                   finishedLine2=" ";
                   finishedLine3=" ";
                   numNodes=0;
                   roadID2 = roadID;
                   //roadID2 = x.roadID;
                   match="";
                   match2="";

              }
              if((roadID.equals(roadID2))||roadID2==""){
                  //finishedLine2+= x.getNodeID();
                  finishedLine2+= newNodeID;
                  //System.out.println( " This is finishedLine2 after inserting newNodeID: "+ newNodeID+"   "+ finishedLine2);
                  //System.out.println(newNodeID + "     this is newNodeID copied to finishedLine2 string."+finishedLine2+" checking to see if newNOdeID was changed in string");
                  //System.out.println(" This is finishedLine1: " +finishedLine1);
                  finishedLine2+="          ";
                  finishedLine2 += finishedLine1;
                  finishedLine2+="              ";
                  //System.out.println( " This is finishedLine2: "+ finishedLine2);
                  numNodes+=1;
                  finishedLine1="";
                  match2="";
                  match="";
                  //System.out.println("this is in if statement");
              }
              finishedLine3 = "";
         }
         else{
              System.out.print(header+"\n");
               bw.write(header + "\n");
               numLines+=1;
          }
          roadID2 = roadID;
          match="";
          match2="";

       }    
       br.close(); 
    }    

}

这是我的输出:

        road_id     #_of_nodes          node_id         node_x_coord        node_y_coord    ... end of list
This is keys:   -86.88642133.035899and this is the :  1  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88651833.035997? 
This is keys:   -86.88642133.035899and this is the :  2  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88657833.03605? 
This is keys:  -86.88651833.035997and this is the :  3  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88657833.03605? 
This is keys:   -86.88642133.035899and this is the :  4  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88666833.03609? 
This is keys:  -86.88651833.035997and this is the :  5  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88666833.03609? 
This is keys:  -86.88657833.03605and this is the :  6  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88666833.03609? 
This is keys:   -86.88642133.035899and this is the :  7  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:  -86.88651833.035997and this is the :  8  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:  -86.88666833.03609and this is the :  9  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:  -86.88657833.03605and this is the :  10  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:   -86.88642133.035899and this is the :  11  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88651833.035997and this is the :  12  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88674233.03608and this is the :  13  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88666833.03609and this is the :  14  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88657833.03605and this is the :  15  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:   -86.88642133.035899and this is the :  16  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88651833.035997and this is the :  17  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88674233.03608and this is the :  18  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88679433.036037and this is the :  19  iteration through the while loop
Is keys:  -86.88679433.036037 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88666833.03609and this is the :  20  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88657833.03605and this is the :  21  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:   -86.88642133.035899and this is the :  22  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88651833.035997and this is the :  23  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88679133.035966and this is the :  24  iteration through the while loop
Is keys:  -86.88679133.035966 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88674233.03608and this is the :  25  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88679433.036037and this is the :  26  iteration through the while loop
Is keys:  -86.88679433.036037 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88666833.03609and this is the :  27  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88657833.03605and this is the :  28  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:   -86.88642133.035899and this is the :  29  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88651833.035997and this is the :  30  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88674333.03591and this is the :  31  iteration through the while loop
Is keys:  -86.88674333.03591 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88679133.035966and this is the :  32  iteration through the while loop
Is keys:  -86.88679133.035966 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88674233.03608and this is the :  33  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88679433.036037and this is the :  34  iteration through the while loop
Is keys:  -86.88679433.036037 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88666833.03609and this is the :  35  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88657833.03605and this is the :  36  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:   -86.88642133.035899and this is the :  37  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88642133.035899? 

感谢任何可以提供帮助的人,希望我能够清晰简洁。

2 个答案:

答案 0 :(得分:0)

使用reporting-officer-id时,通常暗示对象是相同的。因此,请避免将set用于支持==的对象,而是将==用于原语。在您的情况下,您应该使用Object equals(),因为这是两个不同的对象。在上面的问题中,您没有显示可能导致错误行为的==的初始化。

答案 1 :(得分:0)

我假设你复制了你运行的代码的输出。我建议你在比较两个字符串对象时使用Object equals()。但是我在你的代码中看到的是你的密钥中有一个前导空格(“-86.88642133.035899”而不是“-86.88642133.035899”)。因此,您可以在创建HashMap时尝试避免这种情况,或者如果您从某个文件获取输入,则可以修剪字符串,这里是文档的链接 -

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()

希望这有帮助。