通过比较LinkedHashMap中的值来获取密钥

时间:2018-05-07 11:18:29

标签: java netbeans foreach linkedhashmap

  1. 我制作了一个名为(ObjClass)的自定义类。

  2. 我在LinkedHashMap中插入自定义类名而不是值。如下所示:

    LinkedHashMap map = new<> LinkedHashMap的;

  3. 我在ObjClass中有以下getter和setter,名字是

    私人字符串名称;

    private int level;

    private String parent;

  4. 现在的问题是,当我尝试通过比较特定字段来获取密钥时,它没有执行。

    我的代码如下:

            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();
    
        }} 
    

3 个答案:

答案 0 :(得分:1)

因为您只有一个ObjClass个实例,其名称会被覆盖为WHITE

答案 1 :(得分:0)

您只为LinkedHashMap的所有输入提供单个实例。检查更新的代码。

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();

    }

}

您需要重新初始化它。

bb = new ObjClass();

答案 2 :(得分:0)

在Java中,所有对象变量都是引用。结果你的bb变量被最后一个值改变了。请参阅更正后的代码,如下所示:

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();

    }
}

另见Is Java “pass-by-reference” or “pass-by-value”?