使用特定字符串从集合中过滤数据

我只想收到不包含某些特定字符串的数据。

我有这个代码:

@GET @Path("getParticipants") @Produces(MediaType.APPLICATION_JSON) fun getParticipants(): Map<String, List> { val (nodeInfo, nodeUpdates) = rpcOps.networkMapFeed() nodeUpdates.notUsed() return mapOf("getParticipants" to nodeInfo .map { it.legalIdentities.first().toString() } .filter { it != myLegalName && it !in NOTARY_NAMES }) } 

上面运行这个代码时,我收到这个:

 0 "C=GB,L=London,O=UserA" 1 "C=GB,L=London,O=Controller" 2 "C=US,L=New York,O=UserB" 
  • myLegalName =“UserA”
  • NOTARY_NAMES =“控制器”

我想达到的是,getParticipants只检索不包含UserA(“myLegalName”)和NOT CONTAINING Controller(“NOTARY_NAMES”)的行,所以在这种情况下只有“C = US,L = New York ,O =用户B”。

基于你的陈述输出,你的谓词代替你的结果值看起来像这样…

 "C=GB,L=London,O=UserA" != "UserA" && "C=GB,L=London,O=UserA" !in "Controller" // or, after using information from your comments "C=GB,L=London,O=UserA" != "UserA" && "C=GB,L=London,O=UserA" !in listOf("Controller","NetworkSErvice") 

…当然也不会过滤掉你想要的东西。

如果除了legalIdentities.first()之外的其他字段包含所需的信息,则对其进行过滤(并在地图之前调用filter)…

 .filter { it.someOtherField() != myLegalName && it.someOtherField() !in NOTARY_NAMES } //then... .map { it.legalIdentities.first().toString() } 

否则,解析.legalIdentities.first()获取字符串的“O”成员并对其进行过滤。