Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hashmap中重写的值_Java_Oop_Instance_Getter Setter_Setter - Fatal编程技术网

Java hashmap中重写的值

Java hashmap中重写的值,java,oop,instance,getter-setter,setter,Java,Oop,Instance,Getter Setter,Setter,我正在使用一个HashMap来存储一个特定消息中需要的标记列表,用于多个消息,即HashMap。每个TagObject都有属性,主要的属性是某个消息中是否需要该标记。现在,如果在一条消息中需要标记,而在另一条消息中不需要标记,那么最终需要的值就是最后处理的值。我还创建了一个HashMap来存储所有处理过的标记。也许这就是问题的根源?我认为问题在于同一个变量被反复重写,如何为每条消息创建单独的副本以存储在消息的主HashMap中 我已经查过了,但我每次都在创建一个新的字段,所以这似乎不是原因 fo

我正在使用一个
HashMap
来存储一个特定消息中需要的标记列表,用于多个消息,即
HashMap
。每个
TagObject
都有属性,主要的属性是某个消息中是否需要该标记。现在,如果在一条消息中需要标记,而在另一条消息中不需要标记,那么最终需要的值就是最后处理的值。我还创建了一个
HashMap
来存储所有处理过的标记。也许这就是问题的根源?我认为问题在于同一个变量被反复重写,如何为每条消息创建单独的副本以存储在消息的主HashMap中

我已经查过了,但我每次都在创建一个新的字段,所以这似乎不是原因

for(TagObject o:allTags) {
    if(o instanceof Field){
        fieldResult=new Field();
        Field field = (Field) o;
        String required=field.getRequired(); //this is the value found in the file where the specifications are, ie whether the field should be required or no in this message. this value is received correctly
        if(required == null || required.equalsIgnoreCase("yes")) {
            required="true";
        }else{
            required="false";
        }
        else {
            fieldResult=ctd.searchFieldMap(field.name); //find the already created Field object
            fieldResult.setRequired(required); //set the required now as got from the specifications
        }
        fieldsInMessage.add(fieldResult); //add this field to list of fields to be there in the message
        //while being added, I can see the value of the required tag go as in the specs, however when I later print the hashmap of all messages, the tag which was required in one message, but set as not required in another, has changed value to the value of required of the last appearance of the field in the specifications
    }
}

我希望为每条消息创建一个新的字段副本,但似乎所有消息都使用了相同的对象。如何确保每条消息都有一个单独的标记副本,从而具有唯一的属性?

我猜您将标记错误地添加到HashMap中了

例如,如果我想使用消息和标记创建一个结构,我将按照以下代码段执行:

public static void main(String[] args) {
    MessageTagsMap map = new MessageTagsMap();

    map.addNewMessage("Hello World!", "Snippet");
    map.addNewMessage("StackOverflow", "Webpage", "Interesting");

    map.paintMap();

    map.addTagsToMessage("Hello World!", "Deprecated");

    map.paintMap();

}

public class MessageTagsMap {

    HashMap<String, ArrayList<String>> content;

    public MessageTagsMap() {
        content = new HashMap<>();
    }

    void addNewMessage(String message, String... tags) {
        ArrayList<String> messageTags = new ArrayList<>();
        for (String tag : tags) {
            messageTags.add(tag);
        }
        this.content.put(message, messageTags);
    }

    void addTagsToMessage(String message, String... tags) {
        ArrayList<String> messageTags = this.content.get(message);
        for (String tag : tags) {
            messageTags.add(tag);
        }
    }

    void paintMap() {
        for (Map.Entry<String, ArrayList<String>> entry : content.entrySet()) {
            System.out.print("Message: " + entry.getKey() + " tags: {");
            for (String tag : entry.getValue()) {
                System.out.print(tag + " ");
            }
            System.out.println("}");
        }
    }

}
publicstaticvoidmain(字符串[]args){
MessageTagsMap=新建MessageTagsMap();
addNewMessage(“Hello World!”,“Snippet”);
map.addNewMessage(“StackOverflow”、“网页”、“有趣”);
paintMap();
addTagsToMessage(“你好,世界!”,“不推荐”);
paintMap();
}
公共类消息标记映射{
HashMap内容;
公共消息tagsmap(){
content=newhashmap();
}
void addNewMessage(字符串消息、字符串…标记){
ArrayList messageTags=新建ArrayList();
用于(字符串标记:标记){
messageTags.add(tag);
}
this.content.put(message,messageTags);
}
void addTagsToMessage(字符串消息、字符串…标记){
ArrayList messageTags=this.content.get(message);
用于(字符串标记:标记){
messageTags.add(tag);
}
}
void paintMap(){
for(Map.Entry:content.entrySet()){
System.out.print(“消息:“+entry.getKey()+”标记:{”);
for(字符串标记:entry.getValue()){
系统输出打印(标签+“”);
}
System.out.println(“}”);
}
}
}
如您所见,向以前的Map.Entry添加更多信息与向HashMap添加新的Map.Entry不同


当然,如果您每次要向地图添加信息时都创建一个对象的新实例,并使用已存在的相同键放置该信息,那么您就覆盖了地图中的值。

了解了我真正需要的内容。我想在HashMap中创建一个值的副本,我一直在做的是复制对对象的引用,这就是为什么它一直被覆盖的原因。 我真正需要的是一个复制构造函数。
这很有帮助:

如果某条消息中需要或不需要某个标记,那么你为什么要有一个数据结构来存储某条消息中所需的标记列表?嗨,虽然这不是我真正需要的解决方案,可能是因为我的问题本身很模糊,但这确实有助于澄清概念,谢谢你!