草庐IT

collection_items

全部标签

java - 是否有 Java 的 Collections.synchronizedList 的 Kotlin 等价物?或者这在 Kotlin 中是不需要的

在Kotlin中编码,需要一个线程安全列表,如下所述:javaconcurrentArrayListaccessCollections.kt好像没有这个功能。Kotlin的可变列表是否已经是线程安全的?如果没有,我该如何做到这一点?谢谢。 最佳答案 IfItrytousetheJavaListawarningmessagecomesup"Thisclassshouldn'tbeusedinKotlin..."Java列表(和其他集合)是mappedtypes在Kotlin。所以你可以使用Collections.synchronize

collections - 如何在 Kotlin 中无限重复序列?

我想无限重复TSequence中的元素.使用kotlin.collections.asSequence无法做到这一点.例如:valintArray=intArrayOf(1,2,3)valfiniteIntSequence=intArray.asSequence()valmany=10finiteIntSequence.take(many).forEach(::print)//123这不是我想要的。我期待某种kotlin.collections.repeat函数存在,但不存在,所以我自己实现了一个(例如对于这个IntArray):vari=0valinfiniteIntSequence

collections - Java Stream.collect 的 Kotlin 等价物是什么?

如果我想将我的Kotlin集合收集到stdlib中未内置的东西中,我该怎么做? 最佳答案 对于内置操作toList()等未涵盖的场景,您可以使用collect只是折叠的事实。所以给定vallist:List>=listOf("Ann"to19,"John"to23)您可以通过折叠收集到您选择的集合valmap:Map=list.fold(HashMap(),{accumulator,item->accumulator.put(item.first,item.second);accumulator})如果你再定义一个扩展函数funIt

collections - find 和 firstOrNull 有什么区别?

鉴于以下从KotlinKoans中提取的代码:funShop.findAnyCustomerFrom(city:City):Customer?{//Returnacustomerwholivesinthegivencity,ornullifthereisnonereturncustomers.firstOrNull{it.isFrom(city)}}我自己的解决方案使用了customers.find。两者都适用于koan场景。firstOrNull的文档和find看起来很相似。这两个函数有什么区别? 最佳答案 在2014年的这个帖子

Kotlin: "if item not in list"正确的语法

鉴于Kotlin的列表查找语法,if(xinmyList)相对于惯用的Java,if(myList.contains(x))如何表达否定?编译器不喜欢这些:if(xnotinmylist)if!(xinmylist)除了if!(mylist.contains(x)))之外,还有其他惯用的表达方式吗?我没有看到KotlinControlFlowdocs.中提到它 最佳答案 使用x!inlist语法。以下代码:valarr=intArrayOf(1,2,3)if(2!inarr)println("inlist")被编译成等价于:int[

list - Kotlin:如何使用列表强制转换:未经检查的强制转换:kotlin.collections.List<Kotlin.Any?> 到 kotlin.colletions.List<Waypoint>

我想写一个函数来返回List中的每一项。那不是第一个或最后一个项目(通过点)。该函数得到一个通用的List作为输入。仅当列表元素的类型为Waypoint时才应返回结果:fungetViaPoints(list:List):List?{list.forEach{if(it!isWaypoint)returnnull}valwaypointList=listas?List?:returnnullreturnwaypointList.filter{waypointList.indexOf(it)!=0&&waypointList.indexOf(it)!=waypointList.lastI

android - 什么是 "android.R.layout.simple_list_item_1"?

我已经开始学习Android开发,并且正在学习书中的todolist示例://CreatethearraylistoftodoitemsfinalArrayListtodoItems=newArrayList();//CreatethearrayadaptertobindthearraytothelistViewfinalArrayAdapteraa;aa=newArrayAdapter(this,android.R.layout.simple_list_item_1,todoItems);myListView.setAdapter(aa);我无法完全理解这段代码,尤其是这一行:and

安卓微调器 : Get the selected item change event

当所选项目发生变化时,如何为Spinner设置事件监听器?基本上我想做的是类似于这样的事情:spinner1.onSelectionChange=handleSelectionChange;voidhandleSelectionChange(Objectsender){//handleevent} 最佳答案 之前的一些答案是不正确的。它们适用于其他小部件和View,但Spinnerwidget的文档明确指出:Aspinnerdoesnotsupportitemclickevents.Callingthismethodwillrais

python - pymongo 中的 collection.getIndexes() shell 命令的等价物是什么?

我似乎找不到作为pymongo的Collection对象的一部分实现的getIndexes()命令-这是故意的吗?它是在类层次结构的其他地方实现的吗?如果不是,那么获得相同效果的pymongo规范方法是什么? 最佳答案 您可能正在寻找的是index_information()在收藏级别。来自文档:Getinformationonthiscollection’sindexes.Returnsadictionarywherethekeysareindexnames(asreturnedbycreate_index())andtheval

python - pymongo 中的 collection.getIndexes() shell 命令的等价物是什么?

我似乎找不到作为pymongo的Collection对象的一部分实现的getIndexes()命令-这是故意的吗?它是在类层次结构的其他地方实现的吗?如果不是,那么获得相同效果的pymongo规范方法是什么? 最佳答案 您可能正在寻找的是index_information()在收藏级别。来自文档:Getinformationonthiscollection’sindexes.Returnsadictionarywherethekeysareindexnames(asreturnedbycreate_index())andtheval