Java 此方法对Collections.unmodifiableList有用吗?
让我解释一下,我一直在读(难以置信)关于使你的收藏不可修改的书,看看,这是一个有趣的想法,但我无法想象它的实际情况Java 此方法对Collections.unmodifiableList有用吗?,java,Java,让我解释一下,我一直在读(难以置信)关于使你的收藏不可修改的书,看看,这是一个有趣的想法,但我无法想象它的实际情况 有人能解释一下它的实际应用吗 它有两个主要优点: 在编写代码时,您可以创建只读集合类而无需创建子类,您可以在每种类型的集合上使用通用只读包装器(可重用性) 在运行时,您可以创建现有集合的只读视图,而无需将整个集合复制到只读实现(这通常很昂贵) 当您想要阻止类用户修改自己的内部集合时,后者通常很有用 如今,它也被认为是一种很好的设计,非常适合这种模式 示例1: class myCom
有人能解释一下它的实际应用吗 它有两个主要优点:
class myComplicatedCollection<T> implements Collection<T> {
// Code goes here
// O no, I still have to deal with the read-only use-case.
// Instead of duplicating almost all of my code or using inheritance I'll use this handy-dandy wrapper
public Collection<T> readonlyVersion() {
return Collections.unmodifiableCollection(this);
}
}
类MyComplementCollection实现了收集{
//代码在这里
//O不,我仍然需要处理只读用例。
//与其复制几乎所有的代码或使用继承,我将使用这个方便的漂亮包装器
公共集合只读版本(){
返回集合。不可修改的集合(此);
}
}
示例2:
class myClass {
private Collection<T> theData;
// Users need to access the data,
// but we don't want them modifying the private data of this class
public Collection<T> getTheData() {
return Collections.unmodifiableCollection(theData);
}
}
class-myClass{
私人收集数据;
//用户需要访问数据,
//但是我们不希望他们修改这个类的私有数据
公共集合gethedata(){
返回集合。不可修改的集合(数据);
}
}
它有两个主要优点:
class myComplicatedCollection<T> implements Collection<T> {
// Code goes here
// O no, I still have to deal with the read-only use-case.
// Instead of duplicating almost all of my code or using inheritance I'll use this handy-dandy wrapper
public Collection<T> readonlyVersion() {
return Collections.unmodifiableCollection(this);
}
}
类MyComplementCollection实现了收集{
//代码在这里
//O不,我仍然需要处理只读用例。
//与其复制几乎所有的代码或使用继承,我将使用这个方便的漂亮包装器
公共集合只读版本(){
返回集合。不可修改的集合(此);
}
}
示例2:
class myClass {
private Collection<T> theData;
// Users need to access the data,
// but we don't want them modifying the private data of this class
public Collection<T> getTheData() {
return Collections.unmodifiableCollection(theData);
}
}
class-myClass{
私人收集数据;
//用户需要访问数据,
//但是我们不希望他们修改这个类的私有数据
公共集合gethedata(){
返回集合。不可修改的集合(数据);
}
}
当您返回一个属于对象私有成员的列表时,它非常有用,因为从外部修改列表会破坏封装
e、 g.
obj.getList().clear()
将清除obj中的列表(假定getList()返回私有成员),但如果getList()返回传递到Collections.unmodifiableList中的列表,则会引发异常当您返回对象私有成员的列表时,这非常有用,因为从外部修改列表会破坏封装
e、 g.
obj.getList().clear()
将清除obj中的列表(假定getList()返回私有成员),但如果getList()返回传递到Collections.unmodifiableList中的列表,则会引发异常,您可以在希望安全发布列表和/或强制执行不可变性的任何时候使用它
例如:
// neither the reference nor the contents can change
private static final List<String> codes = Collections.unmodifiableList(Arrays.asList("a", "b", "c"));
//引用和内容都不能更改
私有静态最终列表代码=Collections.unmodifiableList(Arrays.asList(“a”、“b”、“c”));
此列表不能在类内修改,可以安全发布:
// the caller can't change the list returned
public static List<String> getCodes() {
return codes;
}
//调用方无法更改返回的列表
公共静态列表getCodes(){
返回码;
}
在您希望安全发布列表和/或强制执行不变性的任何时候使用它
例如:
// neither the reference nor the contents can change
private static final List<String> codes = Collections.unmodifiableList(Arrays.asList("a", "b", "c"));
//引用和内容都不能更改
私有静态最终列表代码=Collections.unmodifiableList(Arrays.asList(“a”、“b”、“c”));
此列表不能在类内修改,可以安全发布:
// the caller can't change the list returned
public static List<String> getCodes() {
return codes;
}
//调用方无法更改返回的列表
公共静态列表getCodes(){
返回码;
}
@Dukeling ty表示改善mi文本。@Dukeling ty表示改善mi文本。