参数化类型的空集合上的 Java 迭代器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821536/
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
Java iterator over an empty collection of a parameterized type
提问by miner49r
In Java, I need to return an Iterator from my method. My data comes from another object which usually can give me an iterator so I can just return that, but in some circumstances the underlying data is null. For consistency, I want to return an "empty" iterator in that case so my callers don't have to test for null.
在 Java 中,我需要从我的方法中返回一个迭代器。我的数据来自另一个对象,它通常可以给我一个迭代器,所以我可以直接返回它,但在某些情况下,基础数据为空。为了一致性,我想在这种情况下返回一个“空”迭代器,这样我的调用者就不必测试 null。
I wanted to write something like:
我想写一些类似的东西:
public Iterator<Foo> iterator() {
if (underlyingData != null) {
return underlyingData.iterator(); // works
} else {
return Collections.emptyList().iterator(); // compiler error
}
}
But the Java compiler complains about the returning Iterator<Object>
instead of Iterator<Foo>
. Casting to (Iterator<Foo>)
doesn't work either.
但是 Java 编译器抱怨返回Iterator<Object>
而不是Iterator<Foo>
. 投射到(Iterator<Foo>)
也不起作用。
采纳答案by Alex B
You can get an empty list of type Foo through the following syntax:
您可以通过以下语法获取 Foo 类型的空列表:
return Collections.<Foo>emptyList().iterator();
回答by miner49r
Sorry, I figured it out. You need to use an assignment so that the compiler can figure out the parameterized type.
对不起,我想通了。您需要使用赋值,以便编译器可以找出参数化类型。
public Iterator<Foo> iterator() {
if (underlyingData != null) {
return underlyingData.iterator();
} else {
List<Foo> empty = Collections.emptyList(); // param type inference
return empty.iterator();
}
}
回答by Carl Manaster
I'd go more along the lines of
我会更倾向于
public Iterator<Foo> iterator() {
if (underlyingData == null)
return Collections.<Foo> emptyList().iterator();
return underlyingData.iterator();
}
Just, handle the special case and return, then handle the normal case. But my main point is that you can avoid the assignment with
只是,处理特殊情况并返回,然后处理正常情况。但我的主要观点是你可以避免分配
Collections.<Foo> emptyList().iterator();
回答by Tom Hawtin - tackline
I guess this shows that Java type inference doesn't work in every case and that the ternary operator is not always equivalent to the apparently equivalent if-else construct.
我想这表明 Java 类型推断并非在所有情况下都有效,并且三元运算符并不总是等效于表面上等效的 if-else 构造。
I also want to state avoid null
. Also avoid passing Iterator
s about, because they have strange stateful behaviour (prefer Iterable
). However, assuming you have a legitimate, non-premature reason for doing this, my preferred way of writing it would be
我也想声明避免null
。还要避免传递Iterator
s,因为它们有奇怪的有状态行为(prefer Iterable
)。然而,假设你有一个合法的、非过早的理由这样做,我更喜欢的写作方式是
public Iterator<Foo> iterator() {
return getUnderlyingData().iterator();
}
private List<Foo> getUnderlyingData() {
if (underlyingData == null) {
return Collections.emptyList();
} else {
return underlyingData;
}
}
IMO, it's better not to insert the inferable type information if it can be inferred (even if it makes your code longer).
IMO,如果可以推断出可推断类型信息,最好不要插入它(即使它会使您的代码更长)。
You are almost certainly going to do it more than once, so insert a getUnderlyingData
method instead of just declaring a local variable.
您几乎肯定会多次这样做,因此插入一个getUnderlyingData
方法而不是仅仅声明一个局部变量。
You are calling iterator
on both results, so do not repeat yourself.
您正在调用iterator
这两个结果,所以不要重复自己。
回答by Kevin Bourrillion
The annoyance of Collections.<Foo>emptyList().iterator()
is the main reason we provide Iterators.emptyIterator()
in google-collections. No type parameter needed, in cases like yours.
的烦恼Collections.<Foo>emptyList().iterator()
是我们Iterators.emptyIterator()
在google-collections 中提供的主要原因。在像您这样的情况下,不需要类型参数。
回答by abhay8nitt
public final class EmptyIterator{
public static Iterator iterator(){
return new Empty();
}
private static class Empty implements Iterator {
public boolean hasNext(){
return false;
}
public Object next(){
throw new NoSuchElementException();
}
public void remove(){
}
}
}
回答by GOTO 0
Java 7 has been out for a long time now. Unless you're developing for a previous Java version, you would return an empty iterator like this:
Java 7 已经发布很长时间了。除非您为以前的 Java 版本开发,否则您将返回一个空迭代器,如下所示:
return Collections.emptyIterator();