Java:HashMap存储与键和值相同的值。

发布于 2024-12-04 03:47:14 字数 1751 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假面具 2024-12-11 03:47:14

我的猜测是 fetchPreDetails 是一个被 setPreParameters() 改变的集合,或者 setPreParameters() 正在改变一些其他共享状态,以便temparrLst 引用的集合在第二次调用 setPreParameters() 时发生更改。即,

List<String> strings = new ArrayList();
strings.add("a");
strings.add("b");
List<String> otherStrings = strings;
otherStrings.add("c");

我希望您的代码假设 strings 将包含“a”和“b”,而 otherStrings 将包含“a”、“b”和“c”。这不是 Java 中对象引用的工作方式。 List行otherStrings = strings; 使 stringsotherStrings 都指向同一个集合,因此使用任一名称进行的更改都会影响同一件事。

编辑:您新发布的代码似乎证明了我的假设。您有一个名为 arrLstData 的变量,您可以在每次调用 setPreParameters() 时清除、填充并返回该变量。每次调用此方法时,您都会返回相同的集合。因此,您只有同一个集合的多个句柄,而不是多个集合。您需要创建一个新集合,并在每次调用 setPreParameters() 时返回它。

再次编辑:也许这会让事情变得更清楚。这就是您正在做的事情:

public static void main(String[] args) {
    Foo f = new Foo();
    List<String> list1 = f.getList("a", "b");
    System.out.println(list1);
    List<String> list2 = f.getList("c", "d");
    System.out.println(list2);
    System.out.println(list1);
}

static class Foo {
    private List<String> myList = new ArrayList<String>();
    public List<String> getList(String... strings) {
        myList.clear();
        myList.addAll(Arrays.asList(strings));
        return myList;
    }
}

请注意,这完全展示了您所描述的行为,解决该问题的正确方法如下所示:

public static void main(String[] args) {
    Foo f = new Foo();
    List<String> list1 = f.getList("a", "b");
    System.out.println(list1);
    List<String> list2 = f.getList("c", "d");
    System.out.println(list2);
    System.out.println(list1);
}

static class Foo {
    public List<String> getList(String... strings) {
        List<String> result = new ArrayList<String>();
        result.addAll(Arrays.asList(strings));
        return result;
    }
}

My guess is that either fetchPreDetails is a collection being mutated by setPreParameters() or else setPreParameters() is mutating some other shared state so that the collection referenced by your temparrLst is being changed on the second call to setPreParameters(). I.e.

List<String> strings = new ArrayList();
strings.add("a");
strings.add("b");
List<String> otherStrings = strings;
otherStrings.add("c");

I expect your code assumes that strings would contain "a" and "b" and that otherStrings would contain "a", "b", and "c". This isn't how object references work in Java. The line List<String> otherStrings = strings; makes both strings and otherStrings 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 to setPreParameters(). 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 call setPreParameters().

Edit again: Maybe this will make it clearer. Here's what you're doing:

public static void main(String[] args) {
    Foo f = new Foo();
    List<String> list1 = f.getList("a", "b");
    System.out.println(list1);
    List<String> list2 = f.getList("c", "d");
    System.out.println(list2);
    System.out.println(list1);
}

static class Foo {
    private List<String> myList = new ArrayList<String>();
    public List<String> getList(String... strings) {
        myList.clear();
        myList.addAll(Arrays.asList(strings));
        return myList;
    }
}

Note that this exhibits exactly the behavior that you're describing, and the correct way to solve it is something like this:

public static void main(String[] args) {
    Foo f = new Foo();
    List<String> list1 = f.getList("a", "b");
    System.out.println(list1);
    List<String> list2 = f.getList("c", "d");
    System.out.println(list2);
    System.out.println(list1);
}

static class Foo {
    public List<String> getList(String... strings) {
        List<String> result = new ArrayList<String>();
        result.addAll(Arrays.asList(strings));
        return result;
    }
}
荒路情人 2024-12-11 03:47:14

您在 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 in temparrLst, now you are clearing the the Lists content, putting new stuff in it and storing it to tempArrLstId.
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.

相权↑美人 2024-12-11 03:47:14

您可能是这样的意思吗?

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GlobalsMess {

    private Map<String, String> lstBarring = new HashMap<String, String>();
    private Map<Integer, String> fetchPreDetails = new HashMap<Integer, String>();

    public GlobalsMess() {
        fetchPreDetails.put(1, "john,vikam,david");
        fetchPreDetails.put(14, "1,2,3");
    }

    public Map<String, String> getLstBarring() {

        List<String> tempKeys = setPreParameters(fetchPreDetails.get(1));
        System.out.println("KEY" + tempKeys);

        List<String> tempIds = setPreParameters(fetchPreDetails.get(14));
        System.out.println("VALUE" + tempIds);

        for (int index = 0; index < tempIds.size(); index++) {
            System.out.println("VALUE IN KEY" + tempKeys.get(index));
            System.out.println("VALUE IN VALUE" + tempIds.get(index));
            this.lstBarring.put(tempKeys.get(index), tempIds.get(index));
        }
        System.out.println("INSIDE ODB....>>>>>>>>>>>>>>" + lstBarring);

        return this.lstBarring;
    }

    public List<String> setPreParameters(String fetchPreDetailsValue) {
        List<String> arrLstData = new ArrayList<String>();
        Collections.addAll(arrLstData, fetchPreDetailsValue.split(","));
        return arrLstData;
    }

    public static void main(String[] args) {
        new GlobalsMess().getLstBarring();
    }
}

输出:

KEY[john, vikam, david]
VALUE[1, 2, 3]
VALUE IN KEYjohn
VALUE IN VALUE1
VALUE IN KEYvikam
VALUE IN VALUE2
VALUE IN KEYdavid
VALUE IN VALUE3
INSIDE ODB....>>>>>>>>>>>>>>{david=3, vikam=2, john=1}

Did you maybe mean something like this?

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GlobalsMess {

    private Map<String, String> lstBarring = new HashMap<String, String>();
    private Map<Integer, String> fetchPreDetails = new HashMap<Integer, String>();

    public GlobalsMess() {
        fetchPreDetails.put(1, "john,vikam,david");
        fetchPreDetails.put(14, "1,2,3");
    }

    public Map<String, String> getLstBarring() {

        List<String> tempKeys = setPreParameters(fetchPreDetails.get(1));
        System.out.println("KEY" + tempKeys);

        List<String> tempIds = setPreParameters(fetchPreDetails.get(14));
        System.out.println("VALUE" + tempIds);

        for (int index = 0; index < tempIds.size(); index++) {
            System.out.println("VALUE IN KEY" + tempKeys.get(index));
            System.out.println("VALUE IN VALUE" + tempIds.get(index));
            this.lstBarring.put(tempKeys.get(index), tempIds.get(index));
        }
        System.out.println("INSIDE ODB....>>>>>>>>>>>>>>" + lstBarring);

        return this.lstBarring;
    }

    public List<String> setPreParameters(String fetchPreDetailsValue) {
        List<String> arrLstData = new ArrayList<String>();
        Collections.addAll(arrLstData, fetchPreDetailsValue.split(","));
        return arrLstData;
    }

    public static void main(String[] args) {
        new GlobalsMess().getLstBarring();
    }
}

Output:

KEY[john, vikam, david]
VALUE[1, 2, 3]
VALUE IN KEYjohn
VALUE IN VALUE1
VALUE IN KEYvikam
VALUE IN VALUE2
VALUE IN KEYdavid
VALUE IN VALUE3
INSIDE ODB....>>>>>>>>>>>>>>{david=3, vikam=2, john=1}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文