Home » Java » com.google.common.collect.Lists.newArrayList throws away my elements

com.google.common.collect.Lists.newArrayList throws away my elements

Posted by: admin August 11, 2018 Leave a comment

Questions:

I have this code:

int size = Iterables.size(components);
log.info("Number of components found (before convert): " +  size);

List<Component> listOfComponents = Lists.newArrayList(components);
size = listOfComponents.size();
log.info("Number of components found (after convert): " +  size);

for (Component component : listOfComponents) {
  log.info("component :  " + component);
}

Where Component is: org.sonatype.nexus.repository.storage.Component

When I run it it prints:

Number of components found (before convert): 2
Number of components found (after convert): 0

So after I convert my iterable to a list the elements are no longer there. Also I never get into the following loop.

why does the elements not get copied to the list, thats what the docs say:

Creates a mutable ArrayList instance containing the given elements; a
very thin shortcut for creating an empty list then calling
Iterables.addAll.

Answers: