使用特定字符串过滤数据

时间:2017-12-15 14:02:44

标签: kotlin

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

我的代码是:

@GET
@Path("getParticipants")
@Produces(MediaType.APPLICATION_JSON)
fun getParticipants(): Map<String, List<String>> {
    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 is =&#34; UserA&#34;
  • NOTARY_NAMES = =&#34; Controller&#34;

我想要的是,getParticipants只检索不包含UserA的行(&#34; myLegalName&#34;)和不包含Controller(&#34; NOTARY_NAMES&#34;),所以在这种情况下只有&#34; C = US,L =纽约,O = UserB&#34;。

1 个答案:

答案 0 :(得分:0)

根据您声明的输出,替换结果值后的谓词看起来像这样......

"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 { it.someOtherField() != myLegalName &&
          it.someOtherField() !in NOTARY_NAMES } //then...
.map { it.legalIdentities.first().toString() }

否则,解析.legalIdentities.first()以获得&#34; O&#34;字符串的成员和过滤器。