[英]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.