繁体   English   中英

Java HashMap迭代未显示键和变量之间的比较

[英]Java HashMap Iteration not showing comparison between key and variable

我试图通过HashMap进行迭代,并且当检测到重复的键(key = node coord)时,我想给具有相同key(coord)的新节点相同的nodeID(Hasmap value)。 这是我的问题代码块和输出。 如您所见,它输出key(keys)和新key(finishedLine3)具有相同的值,但是不会进入“ if”语句并更改nodeID。

历史记录:我在这里与“ ==”进行比较,但也尝试过if(keys.equal(finishedLine3))并获得相同的结果。

我还在输出中显示节点键:-86.88642133.035899节点值:00000001和节点键:-86.88642133.035899节点值:00000010是相同的,但是在迭代过程中,我的逻辑没有允许对这两个精确的比较键进入我的“ if”块。

这是我的代码:

    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? 

感谢任何能够提供帮助并希望我足够清楚和简洁的人。

使用== ,通常意味着对象是相同的。 因此,避免对对象使用==来支持Object equals()而对基本类型使用== 在您的情况下,应使用keys.equals(finishedLine3)因为这是两个不同的对象。 在上面你的问题,你有没有表现出的初始化finishedLine3这可能是导致错误的行为。

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

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

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM