Java 比较集合中的元素并将其设置为该集合的最后一个元素

Java 比较集合中的元素并将其设置为该集合的最后一个元素,java,jakarta-ee,arraylist,collections,Java,Jakarta Ee,Arraylist,Collections,如何实现: 比较集合中的一个元素(不知道索引,因此可以使用contains/equals来比较字符串值),检查是否存在,然后必须向该字符串值添加一些字符(将从其他地方提取),然后将其添加回集合中,使其成为最后一个元素 示例场景: xyz<Collection> contains these values: "abc" "hgj" "jsh" "yjk" if (xyz.contains("jsh")){ then concat "jsh" + " " + randomOtherSt

如何实现:

比较集合中的一个元素(不知道索引,因此可以使用contains/equals来比较字符串值),检查是否存在,然后必须向该字符串值添加一些字符(将从其他地方提取),然后将其添加回集合中,使其成为最后一个元素

示例场景:

xyz<Collection> contains these values:
"abc"
"hgj"
"jsh"
"yjk"

if (xyz.contains("jsh")){
then concat "jsh" + " " + randomOtherStuff
And put it back in the collection to be last element so when printed the order is 
"abc"
"hgj"
"yjk"
"jsh + " " + randomOtherStuff"
}
xyz包含以下值:
“abc”
“hgj”
“jsh”
“yjk”
如果(xyz.contains(“jsh”)){
然后concat“jsh”+“”+random其他东西
并将其放回集合中作为最后一个元素,以便在打印订单时
“abc”
“hgj”
“yjk”
“jsh+”“+randomOtherStuff”
}

提前感谢您的帮助:D

您尝试了什么吗?Collections.binarySearch(xyz,“jsh”)将返回索引。
if (collection.contains("jsh")) {
  collection.remove("jsh"); // remove it, so it can be re-added to the end
  collection.add("jsh " + "asdfgdfgsdfhsjdfh"); // add the new string + some random stuff to the end of the list
}