我正在尝试遍历哈希映射,当检测到重复的键(键=节点坐标)时,我想给具有相同键(坐标)的新节点相同的nodeID(Hasmap值)。这是我的问题代码块和我的输出。如您所见,它输出的键(键)和新键(finishLine3)具有相同的值,但它不会进入"if"语句并更改节点ID。
历史:我在这里与" =="进行比较,但也尝试了if(keys.equal(finishLine3))并得到相同的结果。
我还在输出中显示节点密钥:-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()
希望这有帮助。