While updating the application to Java 8, I encountered a strange problem with google guava newArrayList in several places.
Take a look at this example:
import com.google.common.collect.UnmodifiableIterator; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.BasicAttribute; import java.util.ArrayList; import static com.google.common.collect.Iterators.forEnumeration; import static com.google.common.collect.Lists.newArrayList; public class NewArrayListIssue { public static void main(String[] args) throws NamingException { UnmodifiableIterator<?> elements = forEnumeration(getEnumeration().getAll()); System.out.println("declarefirst = " + newArrayList(elements));
In the first example, when I first take the UnmodifiableIterator into my own variable and then call newArrayList , I get what I expect, these are Iterators values ββcopied to the new List .
In the second example, when forEnumeration goes directly to the newArrayList method, I return a List with the containing iterator (which contains the value).
According to Intellij, he believes that both method calls should be newArrayList(Iterator<? extends E> elements) , but I found that when debugging, the second call really goes into newArrayList(E... elements) .
This only happens when compiling with a Java8-oriented Oracle JDK8. If I'm aiming for 7, it works great.
java guava java-8
ryber
source share