Java:HashMap存储与键和值相同的值。
我在 HashMap 中得到了这个奇怪的输出。
我有两个 ArrayList
我的 HashMap
将仅存储字符串作为键和值对。但密钥本身正在被存储在价值中。我已经检查了我的值数组列表,它正在打印该值。但在放置过程中将其设置为键本身。
代码片段是:
public HashMap<String,String> getLstBarring()
{
ArrayList<String> temparrLst=setPreParameters(fetchPreDetails, 1);
System.out.println("KEY" + temparrLst);
ArrayList<String> tempArrLstId=setPreParameters(fetchPreDetails, 14);
System.out.println("VALUE" +tempArrLstId);
int length=tempArrLstId.size();
for(int index=0;index<length;index++)
{
System.out.println("VALUE IN KEY" + temparrLst.get(index));
System.out.println("VALUE IN VALUE" + tempArrLstId.get(index));
this.lstBarring.put(temparrLst.get(index), tempArrLstId.get(index));
}
System.out.println("INSIDE ODB....>>>>>>>>>>>>>>" + lstBarring);
return this.lstBarring;
}
问题是:
- 第一个 SOP 正确打印所有密钥。
- 第二个SOP是VALUE——正确打印所有值。
- 第三个SOP是VALUE IN KEY----打印所有值。
- 第四个 SOP 是 VALUE IN VALUE——打印所有值。
因此,在每次迭代之后,我在 HashMap
中获取值,值,而它应该是键,值。
这是我的方法:-
public ArrayList<String> setPreParameters(HashMap<Integer,String> fetchPreDetails,int index)
{
switch(index)
{
case 1:
{
arrLstData.clear();
splittedString=fetchPreDetails.get(1).split(",");
Collections.addAll(arrLstData, splittedString);
break;
}
return arrLstData;
请指导我哪里出错了。
I am getting this strange output in HashMap
.
I have two ArrayList<String>
one containing the key and another containing value.
My HashMap<String,String>
will store only string as key and value pair. But key itself is getting stored in value. I have checked my value arraylist, it's printing the value. But during putting it's setting it as key itself.
Code snippet is:
public HashMap<String,String> getLstBarring()
{
ArrayList<String> temparrLst=setPreParameters(fetchPreDetails, 1);
System.out.println("KEY" + temparrLst);
ArrayList<String> tempArrLstId=setPreParameters(fetchPreDetails, 14);
System.out.println("VALUE" +tempArrLstId);
int length=tempArrLstId.size();
for(int index=0;index<length;index++)
{
System.out.println("VALUE IN KEY" + temparrLst.get(index));
System.out.println("VALUE IN VALUE" + tempArrLstId.get(index));
this.lstBarring.put(temparrLst.get(index), tempArrLstId.get(index));
}
System.out.println("INSIDE ODB....>>>>>>>>>>>>>>" + lstBarring);
return this.lstBarring;
}
Problem is:
- 1st SOP is KEY-printing all the key correctly.
- 2nd SOP is VALUE-printing all the value correctly.
- 3rd SOP is VALUE IN KEY----printing all the values.
- 4th SOP is VALUE IN VALUE--printing all the values.
Hence after ever iteration I am getting value,value in HashMap
whereas it should be key,value.
Here's look at my Method:-
public ArrayList<String> setPreParameters(HashMap<Integer,String> fetchPreDetails,int index)
{
switch(index)
{
case 1:
{
arrLstData.clear();
splittedString=fetchPreDetails.get(1).split(",");
Collections.addAll(arrLstData, splittedString);
break;
}
return arrLstData;
Please guide me as to where am I going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是
fetchPreDetails
是一个被setPreParameters()
改变的集合,或者setPreParameters()
正在改变一些其他共享状态,以便temparrLst
引用的集合在第二次调用setPreParameters()
时发生更改。即,我希望您的代码假设
strings
将包含“a”和“b”,而otherStrings
将包含“a”、“b”和“c”。这不是 Java 中对象引用的工作方式。List行otherStrings = strings;
使strings
和otherStrings
都指向同一个集合,因此使用任一名称进行的更改都会影响同一件事。编辑:您新发布的代码似乎证明了我的假设。您有一个名为
arrLstData
的变量,您可以在每次调用setPreParameters()
时清除、填充并返回该变量。每次调用此方法时,您都会返回相同的集合。因此,您只有同一个集合的多个句柄,而不是多个集合。您需要创建一个新集合,并在每次调用setPreParameters()
时返回它。再次编辑:也许这会让事情变得更清楚。这就是您正在做的事情:
请注意,这完全展示了您所描述的行为,解决该问题的正确方法如下所示:
My guess is that either
fetchPreDetails
is a collection being mutated bysetPreParameters()
or elsesetPreParameters()
is mutating some other shared state so that the collection referenced by yourtemparrLst
is being changed on the second call tosetPreParameters()
. I.e.I expect your code assumes that
strings
would contain "a" and "b" and thatotherStrings
would contain "a", "b", and "c". This isn't how object references work in Java. The lineList<String> otherStrings = strings;
makes bothstrings
andotherStrings
point to the same collection, and thus changes made using either name affect the same thing.Edit: Your newly-posted code seems to prove my hypothesis. You have a variable called
arrLstData
that you clear, populate, and return on each call tosetPreParameters()
. You're returning the same collection every time you call this method. Therefore you just have multiple handles to the same collection instead of multiple collections. You need to create a new collection and return it each time you callsetPreParameters()
.Edit again: Maybe this will make it clearer. Here's what you're doing:
Note that this exhibits exactly the behavior that you're describing, and the correct way to solve it is something like this:
您在
setPreParameters
方法中一遍又一遍地重复使用同一个列表。arrLstData
中的列表被返回并存储在temparrLst
中,现在您正在清除列表内容,将新内容放入其中并将其存储到tempArrLstId
>.现在这三个变量都包含完全相同的列表(它们不相等,它是相同的!)。
整个示例中只有一个 List 对象!
就像你有一个盒子,在一侧贴上“A”标签,在里面放东西,在另一侧贴上“B”标签,然后想知道为什么当你把盒子“A”倒过来时,盒子“B”是空的。
You are reusing the same List over and over at your
setPreParameters
Method.The List in
arrLstData
is returned and stored intemparrLst
, now you are clearing the the Lists content, putting new stuff in it and storing it totempArrLstId
.Now the three variables all contain the very same list (they are not equals, its the same!).
There is only one List object at the whole example!
Its like you got a box and label it "A" on one side put stuff in it, label it "B" on another side and wondering why the box "B" is empty when you turn box "A" upside-down.
您可能是这样的意思吗?
输出:
Did you maybe mean something like this?
Output: