0

I have an ArrayList that I want to convert this is my code :

ArrayList<PostWrapper> postWrapperList = listingWrapper.getData();
int size = postWrapperList.size();
ArrayList<Post> postList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
    PostWrapper postWrapper = postWrapperList.get(i);
    if (postWrapper.isKindLink()) {
        Post post = postWrapper.getData();
        postList.add(post);
    }
}
postList.removeAll(Collections.singleton(null));

As you can see in this code I'm doing some kind of filtering so the final size can be smaller than the initial size. Currently I'm calling removeAll() to remove null object to shrink the ArrayList.

My question is which is more efficient, this way or just creating ArrayList without any initial size and then just let the ArrayList class handling the dynamic allocation?
I'm concerned with deallocating excess capacity that I reserved when creating the list.

7
  • What do you mean by "shrink" the list. Are you expecting that some entries you added were actually null, and you want to remove those? Or are you concerned with deallocating excess capacity that you reserved when creating the list? You cannot do the latter with removeAll() since unused capacity is not considered part of the list. The question is not clear enough to be answered yet. Commented Dec 14, 2015 at 4:31
  • @JimGarrison I'm actually concerned with deallocating excess capacity that I reserved when creating the list. So based on your explanation, I don't need to do anything? Commented Dec 14, 2015 at 4:33
  • 1
    The excess capacity is just one reference per slot (4 or 8 bytes depending on the size of a pointer). The list does not store empty instances of Post. Unless the list size is in the hundreds of thousands, don't worry about it. If you really want to do it you have to copy the list to a new one of the correct capacity. BTW, this smells like premature optimization. Commented Dec 14, 2015 at 4:35
  • You can use postList.trimToSize(); to trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance. Commented Dec 14, 2015 at 4:35
  • 1
    And as @WalterM pointed out in his answer, there's always trimToSize() if you really, really need to do it and are sure you won't be adding any more elements. But remember it costs time to do the copy. I'd leave things alone until you can show you actually need to trim it. Commented Dec 14, 2015 at 4:38

2 Answers 2

2

Code from java.util.ArrayList

/**
 * Trims the capacity of this <tt>ArrayList</tt> instance to be the
 * list's current size.  An application can use this operation to minimize
 * the storage of an <tt>ArrayList</tt> instance.
 */
public void trimToSize() {
   modCount++;
   if (size < elementData.length) {
       elementData = (size == 0)
         ? EMPTY_ELEMENTDATA
         : Arrays.copyOf(elementData, size);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand the question correctly, this code snippet might be helpful to you using Java 8's new stream utility:

List<Post> postList = postWrapperList.stream()
                          .filter(PostWrapper::isKindLink)
                          .map(PostWrapper::getData)
                          .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.