我需要实现一个 Immutable List 接口(interface)并修改其方法以确保列表的不变性。
我想我已经成功做到了这一点,但在确保此列表中找到的可变对象的这种不变性方面遇到了困难。
考虑以下界面:
public interface ImmutableListInterface<T>{
T get(int index);
}
及其实现:
public final class ImmutableList<T> implements ImmutableListInterface<T>{
private final List<T> immutableList;
public ImmutableList(List<T> list) {
immutableList = list;
}
@Override
public T get(int index) {
T item;
List<T> temp = new ArrayList<>(immutableList);
try {
//Try retrieving item
item = temp.get(index);
}catch(Exception e) {
System.out.println("Error message: " + e.getMessage());
return null;
}
return item;
}
}
现在,如果我要初始化 MutableObject 类型的 ImmutableList,这不会阻止我修改 MutableObject 的属性。如:
class MutableObject{
public int a;
}
LinkedList<MutableObject> list = new LinkedList<MutableObject>();
MutableObject object = new MutableObject();
object.a = 0;
list.add(object);
ImmutableList<MutableObject> immutable_list = new ImmutableList<MutableObject>(list);
System.out.println("Before:" + immutable_list.get(0).a); //prints 0, as expected
immutable_list.get(0).a = 1;
System.out.println("After:" + immutable_list.get(0).a);//prints 1 - expecting 0
我尝试过将方法get设置为final,但没有成功。
看来我在实现中可能忽略了一些东西。如何才能真正确保列表的不变性,让对象本身保持可变?
最佳答案
问题的原因是 get()
方法返回的对象引用实际列表中的同一对象。因此,对其所做的更改将应用于您的实际列表的对象。
另一方面,你不能确保列表内容的不变性,你只能确保它们的引用不被修改,而它们的值可能会改变。
如果您确实想保留列表中的对象并避免在 get()
之后修改列表的内容,我建议您在方法中返回对象的深拷贝。它将返回具有全新引用的相同对象,该对象不会链接到您的列表。
有几种方法可以做到,更多信息可以查看in this question
关于java - 确保不可变列表中可变对象的不可变性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453228/