gpt4 book ai didi

java - 通过 ; 分割字符串然后比较值

转载 作者:行者123 更新时间:2023-12-03 05:43:14 25 4
gpt4 key购买 nike

我有一个字符串,它返回一堆由 ; 分隔的 id。我将它们分开,将它们各自的值传递给另一个实用程序来查找父 ID。然后,我需要将父 id 相互比较,以确保所有 id 都是相同的值。该字符串可以包含一对多的 id。示例:

String unitIdList = "3e46907f-c4e8-44d2-8cab-4abb5a191a72;9d242306-1c7c-4c95-afde-e1057af9d67c;2e96838f-f0df-4c82-b5bc-cb81a6bdb792;b21a4b19-6c1a-4e74-aa84-7900f6ffa7a8"

for ( String unitIds : unitIdList.split(";") ) {
parentId = UnitUtil.getInstance().getParentId(UUID.fromString(unitIds));

// now I need to compare parentIds. They should all be the same, but if not then do something else.
}

如何比较每个值?

最佳答案

您可以将它们全部放入 Set 中并检查大小是否为 1:

String unitIdList = // ...
Set<String> distinctIds = new HashSet<>(Arrays.asList(unitIdList.split(";")));
if(distinctIds.size() == 1) {
// all the same ids
} else {
// not all the same!
}

关于java - 通过 ; 分割字符串然后比较值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36924694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com