草庐IT

unmodifiableList

全部标签

java - Ruby 相当于 Java 的 Collections.unmodifiableList 和 Collections.unmodifiableMap

Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur

java - unmodifiableList 线程安全吗?

我有一个字符串列表(tagList)需要在多个线程之间共享以供读取,所以我创建了一个不可修改的版本并将其传递给线程,我不确定它是否线程安全,因为线程只读取该列表所以我想应该没问题吧?另外,当我将那个不可修改的列表传递给线程时,它是传递一个副本并由线程共享,还是创建多个副本并将一个副本传递给每个线程?这是我的代码:finalListtList=Collections.unmodifiableList(tagList);List>calls=newArrayList>();FileStatus[]fsta=_fileSystem.listStatus(p);for(FileStatusst

java - 如果我们有原始的,为什么我们可以更改不可修改的列表?

通过查看Collections的代码类,我知道当我们使用unmodifiableList(Listlist)或unmodifiableCollection(Collectionc)方法时,它并没有创建一个新对象,而是返回了引用同一对象并覆盖可以修改List的方法[add,addall,remove,保留所有...]所以我运行了这个测试:ListmodifiableList=newArrayList();modifiableList.add(1);ListunmodifiableList=Collections.unmodifiableList(modifiableList);//unm

java - List.of(...) 或 Collections.unmodifiableList()

如果你有一个Liststrings例如,你会继续写:Collections.unmodifiableList(strings)或切换到:List.of(strings.toArray(newString[strings.size()]))实例化对性能(内存和运行时方面)的初始影响是什么?List.of中是否有运行时优势?变体? 最佳答案 这并不是一个很好的比较,因为这些方法做了不同的事情:Collections::unmodifiable...创建一个不可修改的View。它不是是不可变的,因为如果您更改原始的支持集合(您的示例中的l

java - Collections.unmodifiableList(list) 是否需要锁?

我在名为Products.java的文件中维护了一个productListprivateListproductList=Collections.synchronizedList(newArrayList());现在创建一个同步列表,将确保像添加/删除这样的操作将有一个隐式锁,我不需要显式地锁定这些操作。我公开了一个返回此列表的unmodifiableList的函数。publicListgetProductList(){returnCollections.unmodifiableList(productList);}在我的应用程序中,多个线程可以同时调用这个函数。那么,在将List转换为

java - Collections.unmodifiableList 和防御性副本

如果我写Lista1=Arrays.asList(1,2,3);Lista2=Collections.unmodifiableList(a1);a2是只读的,但如果我写了a1.set(0,10);那么a2也被修改了。如果在API中说:Returnsanunmodifiableviewofthespecifiedcollection.Thismethodallowsmodulestoprovideuserswith"read-only"accesstointernalcollections.那么,为什么如果我修改了原始集合,目标复制的集合也被修改了?也许我误解了它的意思,如果是这样,有什

java - Collections.unmodifiableList 和防御性副本

如果我写Lista1=Arrays.asList(1,2,3);Lista2=Collections.unmodifiableList(a1);a2是只读的,但如果我写了a1.set(0,10);那么a2也被修改了。如果在API中说:Returnsanunmodifiableviewofthespecifiedcollection.Thismethodallowsmodulestoprovideuserswith"read-only"accesstointernalcollections.那么,为什么如果我修改了原始集合,目标复制的集合也被修改了?也许我误解了它的意思,如果是这样,有什

java - google 的 ImmutableList 和 Collections.unmodifiableList() 有什么区别?

来自ImmutableListjavadocs:UnlikeCollections.unmodifiableList(java.util.List),whichisaviewofaseparatecollectionthatcanstillchange,aninstanceofImmutableListcontainsitsownprivatedataandwillneverchange.ImmutableListisconvenientforpublicstaticfinallists("constantlists")andalsoletsyoueasilymakea"defensi

java - google 的 ImmutableList 和 Collections.unmodifiableList() 有什么区别?

来自ImmutableListjavadocs:UnlikeCollections.unmodifiableList(java.util.List),whichisaviewofaseparatecollectionthatcanstillchange,aninstanceofImmutableListcontainsitsownprivatedataandwillneverchange.ImmutableListisconvenientforpublicstaticfinallists("constantlists")andalsoletsyoueasilymakea"defensi

java - 返回 unmodifiableList 是否可以接受,还是应该返回数组?

我有方法ListgetFoos()从远程服务器获取数据并返回。当然,用户不应该更改列表的项目数,因为他会得到与服务器上的数据不同步的数据(如果他想要更改项目数,他有特殊的方法,如addFoo())。第一种方法是返回数组并将方法的签名更改为Foo[]getFoos().但它在java中更常见,用户操作集合更方便,所以我将签名更改为ListgetFoos().这个方法总是返回Collections.unmodifiableList(originalList)因此,当用户尝试更改列表时,他会得到RuntimeException。类似案例中的api设计有什么建议吗?
12