jjzjj

ImmutableList

全部标签

c# - 不变的还是不可变的?

好吧,据我所知,不可变类型本质上是线程安全的,或者我在很多地方都读过,我想我明白为什么会这样。如果实例的内部状态在创建对象后无法修改,那么对实例本身的并发访问似乎没有问题。因此,我可以创建以下List:classImmutableList:IEnumerable{readonlyListinnerList;publicImmutableList(IEnumerablecollection){this.innerList=newList(collection);}publicImmutableList(){this.innerList=newList();}publicImmutable

java - guava的ImmutableList并不是真的不可变

ImmutableList的documentationsays:Althoughthisclassisnotfinal,itcannotbesubclassedasithasnopublicorprotectedconstructors.我知道这有点牵强,但可以在com.google.common.collect包中创建ImmutableList的子类(因为它的构造函数是不是私有(private)的,而是包私有(private)的)这是可变的。从那时起,任何获得对ImmutableList引用的人都不能确定它确实是不可变的。这不会破坏ImmutableList的目的吗?

java - ImmutableList 不扩展 List?

当我深入研究ImmutableList的gs-collection源代码时,它没有扩展java.util.List。然而类javadoc提到所有ImmutableList实现必须实现java.util.List。为什么必须要求实现实现java.util.List而不是ImmutableList本身来扩展java.util.List? 最佳答案 为什么ImmutableList不扩展List?ImmutableCollection不扩展java.util.Collection(并且ImmutableList不扩展java.util.L

java - Guava:copyOf() 方法的 ImmutableList 魔法

想感受一下Guavaguava-libraries的copyOf()方法的“魔力”。有一个我用来检查它的小应用程序。这是documentation:TheJDKprovidesCollections.unmodifiableXXXmethods,butinouropinion,thesecanbeunwieldyandverbose;unpleasanttouseeverywhereyouwanttomakedefensivecopiesunsafe:thereturnedcollectionsareonlytrulyimmutableifnobodyholdsareferenceto

java - 替代通用 Java 方法的非法下界?

我想做以下事情:publicclassImmutableList{publicImmutableListadd(Uelement){...}}也就是说,给定一个不可变列表T,您可以添加任何U到列表以产生一个不可变列表U,约束为U必须是T的父类(superclass)型.例如我可以将一只猴子添加到猴子列表中,生成一个新的猴子列表;我可以将一个人添加到猴子列表中,从而生成一个新的原始人列表(大概是猴子和人类的最小上限);我可以在原始人列表中添加一block石头,生成一个新列表Object(假设岩石和原始人没有其他共同祖先)。这在理论上听起来不错,但下界是U根据JLS是不合法的。我可以改写:

Java 泛型 : Inferring types over two parameters

假设我有一个像这样的简单方法来处理两个列表:publicstaticvoidfoo(Listlist1,Listlist2){}假设我想这样调用它:foo(ImmutableList.of(),ImmutableList.of(1));这不会编译,因为javac不够聪明,无法弄清楚我正在尝试创建两个整数列表。相反,我必须写:foo(ImmutableList.of(),ImmutableList.of(1));我应该如何更改foo的声明以允许第一个版本和第二个版本一样工作? 最佳答案 我很确定Java的类型推断不够强大,无法处理统一

java - 如何设计一个持有 java.lang.String 的不可变值类?

目标创建一个类用作的不可变列表String对象。方法我决定利用GoogleGuava的ImmutableList集合而不是包装一个简单的List与Collections.unmodifiableList(Listlist)因为我知道这避免了对支持List的不必要的并发检查,不知道被包裹(来源:ImmutableCollectionsExplained)。要求类是跨线程使用的“值持有者”不允许任何代码在创建后更改内部值锦上添花类应该实现Iterable按创建顺序迭代值一组给定的String应该只有一个类s.尝试这里有一些尝试,尽管更多的组合是可能的。请原谅幽默的演绎。尝试#1(包括使用示

java - ImmutableList.builder() 错误?

我刚开始学习Guava,我注意到ImmutableList.builder()有一些特别之处.这不编译:ListiList=ImmutableList.builder().add("One").add("Two").build();//Typemismatch:cannotconvertfromListtoList这个有效:ListiList=newImmutableList.Builder().add("One").add("Two").build();我可以忍受使用newImmutableList.Builder()但这是ImmutableList.builder()的错误吗??

Java volatile 变量问题

阅读此DZonearticle关于Java并发我想知道是否有以下代码:privatevolatileListlist;privatefinalLocklock=newReentrantLock();publicvoidupdate(ListnewList){ImmutableListl=newImmutableList().addAll(newList);lock.lock();list=l;lock.unlock();}publicListget(){returnlist;}相当于:privatevolatileListlist;publicvoidupdate(ListnewLis

java - Guava :ImmutableList.of(E[]) 的最佳实践

我刚刚注意到ImmutableList.of(E[])已弃用,取而代之的是ImmutableList.copyOf(),原因很明显,如果在其他地方使用原始数组,则无法真正使列表不可变。如果您有一个返回数组的方法,并且您知道该方法不保留对数组的引用,并且您的代码除了传递之外不保留对数组的引用,该怎么办它到ImmutableList.of()?我应该...继续使用ImmutableList.of(E[])(这似乎是个坏主意,因为该方法将消失)使用Collections.unmodifiableList(Arrays.asList())使用ImmutableList.copyOf()——这似