Java 检测最后一个 foreach 循环迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41591107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detect last foreach loop iteration
提问by mhery
Supposing that I have some foreach
loop like this:
假设我有一些这样的foreach
循环:
Set<String> names = new HashSet<>();
//some code
for (String name: names) {
//some code
}
Is there a way to check inside foreach
that the actual name is the last one in Set
without a counter?
有没有办法foreach
在Set
没有计数器的情况下检查内部实际名称是否是最后一个?
(Sorry if it's a duplicated question, I didn't found here some question like this)
(对不起,如果这是一个重复的问题,我没有在这里找到这样的问题)
采纳答案by Lazar Petrovic
There isn't, take a look at How does the Java 'for each' loop work?
没有,看看Java 'for each' 循环是如何工作的?
You must change your loop to use an iterator explicitly or an int counter.
您必须更改循环以显式使用迭代器或 int 计数器。
回答by Murat Karag?z
There is no build in method to check if the current element is also the last element. Besides that you are using a HashSet
which does not guarantee the return order. Even if you want to check it e.g. with an index i
the last element could always be a different one.
没有内置方法来检查当前元素是否也是最后一个元素。除此之外,您使用的HashSet
是不能保证退货单的。即使您想检查它,例如使用索引i
,最后一个元素也可能总是不同的。
回答by CraigR8806
A Set
does not guaranty order over of items within it. You may loop through the Set
once and retrieve "abc" as the "last item" and the next time you may find that "hij" is the "last item" in the Set
.
ASet
不保证其中的项目的顺序。您可以遍历Set
一次并检索“abc”作为“最后一项”,下次您可能会发现“hij”是Set
.
That being said, if you are not concerned about order and you just want to know what the arbitrary "last item" is when observing the Set
at that current moment, there is not built in way to do this. You would have to make use of a counter.
话虽如此,如果您不关心顺序,而只想知道在观察Set
当前时刻时任意的“最后一项”是什么,则没有内置的方法可以做到这一点。您将不得不使用计数器。
回答by Edu G
Other answears are completely adequate, just adding this solution for the given question.
其他答案完全足够,只需为给定的问题添加此解决方案即可。
Set<String> names = new HashSet<>();
//some code
int i = 0;
for (String name: names) {
if(i++ == names.size() - 1){
// Last iteration
}
//some code
}
回答by Joao Pereira
For simplicity and understandability, imo, would do:
为了简单和易懂,imo 会这样做:
Set<String> names = new HashSet<>();
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
//Do stuff
if (!iterator.hasNext()) {
//last name
}
}
Also, it depends on what you're trying to achieve. Let's say you are implementing the common use case of separating each name by coma, but not add an empty coma at the end:
此外,这取决于您要实现的目标。假设您正在实现通过 coma 分隔每个名称的常见用例,但未在末尾添加一个空的 coma:
Set<String> names = new HashSet<>();
names.add("Joao");
names.add("Pereira");
//if the result should be Joao, Pereira then something like this may work
String result = names.stream().collect(Collectors.joining(", "));
回答by luk4
.map(String::toString) from the answer above is redundant, because HashSet already contains String values. Do not use Set to concatenate strings because the order is not assured.
上面答案中的 .map(String::toString) 是多余的,因为 HashSet 已经包含 String 值。不要使用 Set 来连接字符串,因为顺序是不确定的。
List<String> nameList = Arrays.asList("Michael", "Kate", "Tom");
String result = nameList.stream().collect(Collectors.joining(", "));
回答by Manoj G
If you are working with a complex object and not just a plain list/set the below code might help. Just adding a map function to actually get the desired string before you collect.
如果您正在处理一个复杂的对象,而不仅仅是一个简单的列表/设置,那么下面的代码可能会有所帮助。只需添加一个映射函数即可在收集之前实际获取所需的字符串。
String result = violations.stream().map(e->e.getMessage()).collect(Collectors.joining(", "));