HashMap代码没有给出想要的结果 [英] HashMap code not giving the desired results
问题描述
我创建了一个散列映射表,其中每个条目对应3个值
关键对象值(这是两个数字)
我创建了一个类,其对象创建并将结果存储在哈希映射中。
这是我的代码,我在其中比较了我的传入数据和哈希映射中以前的值。如果出现相同的数据,那么我只是递增该数据的计数器。问题是它多次打印出相同的数据。为什么会这样?
class Info {
int counter;
字符串数据;
}
Info info = new Info();
int i = 0;
int lines = 0;
HashMap< String,Info> hMap = new HashMap< String,Info>();
尝试{
BufferedReader reader =
新BufferedReader(新FileReader(E:\\write1.txt));
String line = null; ((line = reader.readLine())!= null)
lines ++;
if(line.startsWith(Data:))
{
String comingdata = line.replaceAll(Data:,);
if(hMap.isEmpty())//首次将数据放入散列表
{
info.counter = 1;
info.Data = comingdata;
hMap.put(1,info);
}
else
{
int m = 0;
//每次运行到
//如果值已经存在,则检查递增计数器
for(i = 1; i <= hMap.size(); i ++)
{
String skey = Integer.toString(i);
if(hMap.get(skey).Data.equals(comingdata))
{
Map.get(skey).counter = hMap.get(skey).counter +1;
m = 2;
$ b $ //在hashmap中创建新条目
if(m == 0)
{
String skey = Integer.toString(hMap。大小()+ 1);
info.counter = 1;
info.Data = comingdata;
hMap.put(skey,info);
}
} // else end
} // if end end
} // while end
} // try end
catch(Exception e) {}
for(i = 1; i< = hMap.size(); i ++)
{
String skey = Integer.toString(i);
System.out.println(hMap.get(skey).Data);
System.out.println(hMap.get(skey).counter);
System.out.println(\\\
);
code> Info 对象,所以 HashMap
中的所有键映射到同一个对象。当你在地图中插入任何东西时,你应该创建一个新的 Info
对象。
I have created a hash map in which each entry corresponds to 3 values Key object values ( which are two in number)
I have created a class, whose object I create and store the results in a hash map. This is my code below in which I compare my incoming data with the previous values in the hash map. If the same data comes then I just increment the counter of that data. Problem is that it prints out the same data multiple times. Why is that?
class Info {
int counter;
String Data;
}
Info info = new Info();
int i=0;
int lines=0;
HashMap<String, Info> hMap = new HashMap<String, Info>();
try {
BufferedReader reader =
new BufferedReader(new FileReader("E:\\write1.txt"));
String line = null;
while ((line = reader.readLine()) != null)
{
lines++;
if(line.startsWith("Data:"))
{
String comingdata = line.replaceAll("Data:","");
if(hMap.isEmpty()) // first time to put data in hashmap
{
info.counter=1;
info.Data=comingdata;
hMap.put("1",info);
}
else
{
int m=0;
// everytime it will run to
// check to increment the counter if the value already exists
for(i=1;i<=hMap.size();i++)
{
String skey = Integer.toString(i);
if(hMap.get(skey).Data.equals(comingdata))
{
hMap.get(skey).counter= hMap.get(skey).counter+1;
m=2;
}
}
// making new entry in hashmap
if(m==0)
{
String skey = Integer.toString(hMap.size()+1);
info.counter=1;
info.Data=comingdata;
hMap.put(skey,info);
}
}// else end
} //if end
} //while end
} //try end
catch (Exception e) { }
for(i=1;i<=hMap.size();i++)
{
String skey= Integer.toString(i);
System.out.println(hMap.get(skey).Data);
System.out.println(hMap.get(skey).counter);
System.out.println("\n");
}
You only ever create one Info
object, so all keys in the HashMap
map to the same object. When you insert anything into the map, you should create a new Info
object.
这篇关于HashMap代码没有给出想要的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!