Win*_*ins 6 java collections gs-collections
当我深入研究gs-collection源代码时ImmutableList,它不会扩展java.util.List.但是,类javadoc提到All ImmutableList实现必须实现java.util.List.
为什么必须要求实现实现java.util.List而不是ImmutableList自己扩展java.util.List?
Cra*_*lin 12
为什么不ImmutableList延长List?
ImmutableCollection不扩展java.util.Collection(并且ImmutableList不扩展java.util.List),因为Collection有像add()和的变异方法remove().如果不可变集合具有这些方法,则它们总是必须抛出UnsupportedOperationException.对于不可改变的集合的用户,这将是奇怪地看到add(),并remove()在自动完成选择,如可调用的方法.
为什么Javadoc强制要求所有ImmutableList实现都实现的合同List?
归结为平等.一个ImmutableList应该等于一个List,假设两个列表具有相同的顺序相同的内容.List.equals()强制执行Javadoc合同,其中规定:
当且仅当指定的对象也是列表时,返回true,两个列表具有相同的大小,并且两个列表中的所有对应元素对都相等.
"指定的对象也是一个列表是什么意思?" 我们可以看出AbstractList.equals()它意味着instanceof List.
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
...
}
Run Code Online (Sandbox Code Playgroud)
因此,所有ImmutableList的实现也必须实现List对equals()以对称的方式工作.不可变集合工厂已经隐藏了实现细节,例如具有单个元素的不可变列表由a实现ImmutableSingletonList.它还最终隐藏了List界面.

互操作
这种设计的一个好处是ImmutableList可以强制转换List为与现有API互操作的重要性.
// Library method - cannot refactor the parameter type
public void printAll(List<?> list)
{
for (Object each : list)
{
System.out.println(each);
}
}
ImmutableList<Integer> immutableList = Lists.immutable.with(1, 2, 3);
List<Integer> castList = immutableList.castToList();
printAll(castList);
// also works
printAll((List<?>) immutableList);
// throws UnsupportedOperationException
castList.add(4);
Run Code Online (Sandbox Code Playgroud)
注意:我是GS Collections的开发人员.
| 归档时间: |
|
| 查看次数: |
1186 次 |
| 最近记录: |