哈希图中的值被覆盖

Value overridden in hashmap

我正在使用 HashMap 来存储特定消息所需的标签列表,对于多条消息,即 HashMap<String,ArrayList<TagObject>>。每个 TagObject 都有属性,主要是某个消息中是否需要该标记。现在,如果一条消息中需要一个标签,而另一条消息中不需要,则最终需要的值将是最后处理的值。我还创建了一个 HashMap<String,TagObject> 来存储所有处理过的标签。也许这是导致问题的原因?我认为问题是同一个变量被重复覆盖,我如何为每条消息制作单独的副本以存储在消息的主 HashMap 中?

我查过HashMap overridden by last value,但我每次都在创建一个新的字段,所以这似乎不是原因。

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

}

如您所见,向先前的 Map.Entry 添加更多信息与向 HashMap 添加新的 Map.Entry 是不一样的。

当然,如果您每次要向 Map 添加信息时都创建一个对象的新实例,并且将该信息放入已存在的相同键中,那么您将覆盖地图.

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