-1
  1. I made an Custom Class which name is (ObjClass).

  2. I insert Custom Class name instead of value in LinkedHashMap. Like the following:

    LinkedHashMap map = new <> LinkedHashMap;

  3. I have following getter and setter in ObjClass, there names are

    private String Name;

    private int level;

    private String parent;

Now the problem is when i try to get the key by comparing of specific field like name it didn't execute.

My code is following:

        public class Practice {
     LinkedHashMap<Integer,ObjClass> map = new LinkedHashMap<>();
    public void addd(){
       int a = 10;
            int b = 20;
            int c = 30;
            ObjClass bb = new ObjClass();

bb.setName("COLOR");
       bb.setParent("ROOT");
       bb.setLevel(1);
       bb.getName();
       bb.getParent();
       bb.getLevel();
       map.put(a, bb);

bb.setName("RED");
bb.setParent("COLOR");
bb.setLevel(2);       
bb.getName();
bb.getParent();
bb.getLevel();
map.put(b, bb);

bb.setName("WHITE");
bb.setParent("COLOR");
bb.setLevel(2);        
bb.getName();
bb.getParent();
bb.getLevel();
map.put(c, bb); here



    // the problem is in the for loop i try to get the key of "red" which is 20 but my loop is not working..

    for (Map.Entry<Integer, ObjClass> entry : map.entrySet()) {
        if(entry.getValue().getName().equals("RED"))
        {
            System.out.println("yes"+ entry.getKey());

        }
    }
    System.out.println("    "+map.size());

    }
        public static void main(String[] args) {

               Practice pp = new Practice();
               pp.addd();

    }} 
naeem
  • 43
  • 8
  • I tried to get the key of red by comparing entry.setValue.getName but it didn.t working, it should return the key which is 20. – naeem May 07 '18 at 11:21
  • If you want to access your map values by name, why don't you put the name as keys? – vanje May 07 '18 at 11:22
  • @vanje that is easy but unfortunately i am working on a project it's the requirement thats why i want to get the key by entering a specific value like "red" that is mentioned in the above code and it will return the key of "red". – naeem May 07 '18 at 11:24
  • 1
    You put always the same bb instance to map. You need to create a new instance of bb. Java does not create a copy when inserts something to a collection. – Zaboj Campula May 07 '18 at 12:35
  • I am new to java, thanks @ZabojCampula. please take a look also this problem https://stackoverflow.com/questions/50238904/retrieve-multilevel-jtree-from-database-table – naeem May 08 '18 at 19:09

3 Answers3

1

Because you have only one instance of ObjClass which it's name is overwritten as WHITE

Dennis C
  • 24,511
  • 12
  • 71
  • 99
0

You are providing only single instance to all the inputs of the LinkedHashMap. Check the updated code.

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    Map<Integer, ObjClass> lMap  = new LinkedHashMap<>();
    public void addd(){
       int a = 10;
            int b = 20;
            int c = 30;
            ObjClass bb = new ObjClass();

bb.setName("COLOR");
       bb.setParent("ROOT");
       bb.setLevel(1);
       bb.getName();
       bb.getParent();
       bb.getLevel();
       lMap.put(a, bb);

       bb = new ObjClass();       
bb.setName("RED");
bb.setParent("COLOR");
bb.setLevel(2);       
bb.getName();
bb.getParent();
bb.getLevel();
lMap.put(b, bb);

bb = new ObjClass();
bb.setName("WHITE");
bb.setParent("COLOR");
bb.setLevel(2);        
bb.getName();
bb.getParent();
bb.getLevel();
lMap.put(c, bb);


    // the problem is in the for loop i try to get the key of "red" which is 20 but my loop is not working..

    for (Map.Entry<Integer, ObjClass> entry : lMap.entrySet()) {
        //System.out.println(entry.getKey() + "-" + entry.getValue().getName());
        if(entry.getValue().getName().equals("RED"))
        {
            System.out.println("yes"+ entry.getKey());

        }
    }
    System.out.println("    "+lMap.size());

    }
        public static void main(String[] args) {

               Main pp = new Main();
               pp.addd();

    }

}

You need to initialize it again.

bb = new ObjClass();
Harshit
  • 5,147
  • 9
  • 46
  • 93
0

In Java when all object variable are references. As a result you bb variable get altered with last value. See the corrected code as follows:

import java.util.LinkedHashMap;
import java.util.Map;

public class Practice {
    LinkedHashMap<Integer, ObjClass> map = new LinkedHashMap<>();

    public void addd() {
        int a = 10;
        int b = 20;
        int c = 30;
        ObjClass bb = new ObjClass();

        bb.setName("COLOR");
        bb.setParent("ROOT");
        bb.setLevel(1);
        bb.getName();
        bb.getParent();
        bb.getLevel();
        map.put(a, bb);

        bb = new ObjClass();

        bb.setName("RED");
        bb.setParent("COLOR");
        bb.setLevel(2);
        bb.getName();
        bb.getParent();
        bb.getLevel();
        map.put(b, bb);

        bb = new ObjClass();
        bb.setName("WHITE");
        bb.setParent("COLOR");
        bb.setLevel(2);
        bb.getName();
        bb.getParent();
        bb.getLevel();
        map.put(c, bb);

        // the problem is in the for loop i try to get the key of "red" which is
        // 20 but my loop is not working..

        for (Map.Entry<Integer, ObjClass> entry : map.entrySet()) {
            if (entry.getValue().getName().equals("RED")) {
                System.out.println("yes" + entry.getKey());

            }
        }
        System.out.println("    " + map.size());

    }

    public static void main(String[] args) {

        Practice pp = new Practice();
        pp.addd();

    }
}

also see Is Java “pass-by-reference” or “pass-by-value”?

S. M. Mohiuddin
  • 378
  • 3
  • 10