Java-使用大小初始化ArrayList,然后收缩

Java-使用大小初始化ArrayList,然后收缩,java,arraylist,Java,Arraylist,我有一个要转换的ArrayList这是我的代码: ArrayList<PostWrapper> postWrapperList = listingWrapper.getData(); int size = postWrapperList.size(); ArrayList<Post> postList = new ArrayList<>(size); for (int i = 0; i < size; i++) { PostWrapper pos

我有一个要转换的ArrayList这是我的代码:

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));
ArrayList postWrapperList=listingWrapper.getData();
int size=postWrapperList.size();
ArrayList postList=新的ArrayList(大小);
对于(int i=0;i
正如您在这段代码中看到的,我正在进行某种过滤,以便最终大小可以小于初始大小。目前,我正在调用removeAll()来删除null对象以缩小ArrayList

我的问题是,这样做更有效,还是只创建没有任何初始大小的ArrayList,然后让ArrayList类处理动态分配?

我关心的是释放创建列表时保留的多余容量。

来自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);
   }
}
/**
*将此ArrayList实例的容量修剪为
*列表的当前大小。应用程序可以使用此操作来最小化
*ArrayList实例的存储。
*/
公共空间trimToSize(){
modCount++;
if(大小<元素数据长度){
elementData=(大小==0)
?空元素数据
:Arrays.copyOf(elementData,size);
}
}

来自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);
   }
}
/**
*将此ArrayList实例的容量修剪为
*列表的当前大小。应用程序可以使用此操作来最小化
*ArrayList实例的存储。
*/
公共空间trimToSize(){
modCount++;
if(大小<元素数据长度){
elementData=(大小==0)
?空元素数据
:Arrays.copyOf(elementData,size);
}
}

如果我正确理解了这个问题,这个代码片段可能会对您使用Java 8的新流实用程序有所帮助:

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

如果我正确理解了这个问题,这个代码片段可能会对您使用Java 8的新流实用程序有所帮助:

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

您所说的“缩小”列表是什么意思。您是否希望添加的某些条目实际上为空,并希望删除这些条目?或者,您是否关心释放创建列表时保留的多余容量?您不能使用
removeAll()
执行后者,因为未使用的容量不被视为列表的一部分。这个问题还不够清楚,无法回答。@JimGarrison我实际上关心的是如何释放创建列表时保留的多余容量。因此,根据您的解释,我不需要做任何事情?多余的容量只是每个插槽一个引用(4或8字节,取决于指针的大小)。该列表不存储
Post
的空实例。除非列表的大小是几十万,否则不要担心。如果你真的想这样做,你必须将列表复制到一个容量正确的新列表中。顺便说一句,这听起来像是过早的优化。你可以使用postList.trimToSize();将此ArrayList实例的容量修剪为列表的当前大小。应用程序可以使用此操作来最小化ArrayList实例的存储。正如@WalterM在他的回答中指出的,如果您确实需要这样做,并且确信不会再添加任何元素,那么总是会有
trimToSize()
。但请记住,复制需要时间。我会把事情放在一边,直到你能证明你确实需要修剪它。你所说的“缩小”列表是什么意思。您是否希望添加的某些条目实际上为空,并希望删除这些条目?或者,您是否关心释放创建列表时保留的多余容量?您不能使用
removeAll()
执行后者,因为未使用的容量不被视为列表的一部分。这个问题还不够清楚,无法回答。@JimGarrison我实际上关心的是如何释放创建列表时保留的多余容量。因此,根据您的解释,我不需要做任何事情?多余的容量只是每个插槽一个引用(4或8字节,取决于指针的大小)。该列表不存储
Post
的空实例。除非列表的大小是几十万,否则不要担心。如果你真的想这样做,你必须将列表复制到一个容量正确的新列表中。顺便说一句,这听起来像是过早的优化。你可以使用postList.trimToSize();将此ArrayList实例的容量修剪为列表的当前大小。应用程序可以使用此操作来最小化ArrayList实例的存储。正如@WalterM在他的回答中指出的,如果您确实需要这样做,并且确信不会再添加任何元素,那么总是会有
trimToSize()
。但请记住,复制需要时间。我会把事情放在一边,直到你能证明你真的需要修剪它。