实现保存(或提交)和回滚到Map的方法 [英] Implements save (or commit) and rollback methods to a Map

查看:57
本文介绍了实现保存(或提交)和回滚到Map的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种快速便捷的方法来将save()和rollback()添加到标准Map.假设我有一个Table类的对象"table",而该对象又有一个称为"rows"的私有Map.我想要实现的是一种 fast 并且没有内存浪费的方法让Row可以执行以下操作:

I'm searching for a fast and convenient way to add save() and rollback() to a standard Map. Let's say i have an object 'table' of class Table which in turn has a private Map called 'rows'. What I'm trying to achieve is a fast and without memory waste method for Row to do something like:

row = new Row();
table.addRow(row).setValue("col1", "foo").setValue("col2", "bar").save();
row.setValue("col2", "beer");
System.out.println(table.getRows()); // 1. col1=foo, col2=bar

row.save();
System.out.println(table.getRows()); // 1. col1=foo, col2=beer

实际上,我的设计很简单:当调用addRow()时,我将put()放置在地图中;没有缓冲区,没有临时元素;我只是将整个Row实例传递给rows集合.但是我需要一种快速的方法,并且(如果可能的话)要避免重复行.

Actually, my design is quite trivial: when addRow() is called, i put() the row inside the map; no buffer, no temp elements; i simply pass the entire Row instance to rows collection. But I need a fast method and (if possible) avoiding the duplication of rows.

有什么主意吗?

推荐答案

这听起来像是我想在内存中保存新旧值,但我不想在内存中保存新旧值".

This sounds too much like "I want to have the new and old values in memory, but I do not want to have the new and old values in memory".

选项:

a)所有已添加元素的映射,保存时执行 putAll .

a) A map of all the added elements, when save do putAll.

b)您的地图保存的是< ClassKey,ClassValue2> ,而不是< ClassKey,ClassValue> . Value2 包含两个 ClassValue 项,即新实例和旧实例.在 save (保存)中,您将新的(如果有的话)传递给旧的.仅当您更改每个交易"中的大多数条目时,它才有用.

b) Your map, instead of <ClassKey, ClassValue>, holds <ClassKey, ClassValue2>. Value2 holds two items of ClassValue, the new and old instance. At save, you pass the new one (if any) to the old one. It will be useful only if you are changing most of the entries in each "transaction".

未提及删除元素的问题,这将带给您更多的乐趣.使用选项2,您可以在 Value2 处设置布尔值,使用选项a,您将需要更多解决方法.

Not mentioned is the issue of deleting elements, which will bring you yet more joy. With option 2 you can set a boolean at Value2, with option a you will need more workarounds.

这篇关于实现保存(或提交)和回滚到Map的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆