简体   繁体   English

在Java中将Map键值从一个类复制到另一个类

[英]Copying Map key value from one class to another in Java

Please could anybody help me with copying a Map key value from one class to another in Java.请有人帮我在 Java 中将 Map 键值从一个类复制到另一个类。

Example:例子:

            public class FirstClass
            {   
               public static Map<String, Integer> firstMethod()
               {
                  Map<String, Integer> firstMap = new HashMap <String, Integer>();
                  firstMap.put("1234", 1000000);
                  firstMap.put("2345", 2000000);
                  firstMap.put("3456", 3000000);
                  firstMap.put("4567", 4000000);
                  return firstMap;
               }   
            }

   public class SecondClass

           public SecondClass()
           {
             instanceSet = new HashSet<ThirdClass>();
           }

      public void addInstance(String string)
       {
         ThirdClass integer = new ThirdClass(string);
         instanceSet.add(integer);     
       }   

       public void someMethod(Map<String, Integer> secondMap)
               {
                  for (ThirdClass instance : instanceSet)
                  {
                     if(secondMap.containsKey(instance.getinstanceNumber()))
                     {
                       //what should go in here??
                     }  
                  }   
               }  

Essentially, if the instanceSet contains a key that is found in firstMap then how can the Integer value of that key be extracted and copied to the Integer value of the same key in secondMap?本质上,如果 instanceSet 包含在 firstMap 中找到的键,那么如何提取该键的 Integer 值并将其复制到 secondMap 中相同键的 Integer 值?

I have looked around online but all of the code examples that I have found don't seem to work presumably because the maps are stored in separate classes.我在网上环顾四周,但我发现的所有代码示例似乎都不起作用,因为地图存储在单独的类中。

Something like this code.像这样的代码。 You need the reference of firstMap in your someMethod or vice versa depending upon from where you want to copy to where.您需要在someMethod引用firstMap ,反之亦然,具体取决于您要从哪里复制到哪里。

if(secondMap.containsKey(instance.getinstanceNumber())) {
       firstMap.put(instance.getinstanceNumber(), secondMap.get(instance.getinstanceNumber()));
} 

Something like this -像这样的东西——

public class FirstClass {
    public Map<String, Integer> firstMap;

    public FirstClass() {
        firstMap = new HashMap <String, Integer>();
        firstMap.put("1234", 1000000);
        firstMap.put("2345", 2000000);
        firstMap.put("3456", 3000000);
        firstMap.put("4567", 4000000);
    }

    public void updateMap(String key, Integer value) {
        firstMap.put(key, value);
    }
}

public class SecondClass {

    private Set<ThirdClass> instanceSet;
    private FirstClass firstClass;

    public SecondClass() {
        instanceSet = new HashSet<ThirdClass>();
        firstClass = new FirstClass();
    }

    public void addInstance(String string) {
        ThirdClass integer = new ThirdClass(string);
        instanceSet.add(integer);
    }

    public void someMethod(Map<String, Integer> secondMap) {
        for (ThirdClass instance : instanceSet) {
            if (secondMap.containsKey(instance.getinstanceNumber())) {
                firstClass.updateMap(instance.getinstanceNumber(), secondMap.get(instance.getinstanceNumber()));
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM