亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java流在創建不可變列表時是否會創建一些臨時列表?

java流在創建不可變列表時是否會創建一些臨時列表?

慕森王 2022-05-21 17:03:25
由于不能將任何元素添加到不可變列表中,我認為 java 流首先將元素收集到一個列表中,然后使用第一個列表中的元素創建一個新的不可變列表。因此,列表有兩個實例,第一個實例可用于垃圾收集。所以,我的問題是如上所述,流是否實際上創建了兩個列表對象?如果不是,流如何創建不可變列表?
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

考慮以下示例:


List<String> people

         = getPeople().stream()

                      .collect(collectingAndThen(toList(), Collections::unmodifiableList));

對于這個例子,我使用的是Collections::unmodifiableList方法,所以讓我們檢查源代碼:


/**

 * Returns an unmodifiable view of the specified list.  This method allows

 * modules to provide users with "read-only" access to internal

 * lists.  Query operations on the returned list "read through" to the

 * specified list, and attempts to modify the returned list, whether

 * direct or via its iterator, result in an

 * <tt>UnsupportedOperationException</tt>.<p>

 *

 * The returned list will be serializable if the specified list

 * is serializable. Similarly, the returned list will implement

 * {@link RandomAccess} if the specified list does.

 *

 * @param  list the list for which an unmodifiable view is to be returned.

 * @return an unmodifiable view of the specified list.

 */

public static <T> List<T> unmodifiableList(List<? extends T> list) {

    return (list instanceof RandomAccess ?

            new UnmodifiableRandomAccessList<>(list) :

            new UnmodifiableList<>(list));

}

正如@Pshemo 在評論中提到的那樣,它UnmodifiableList可以作為列表的包裝器,您還可以在源代碼中檢查該類包含一個列表:


 static class UnmodifiableList<E> extends UnmodifiableCollection<E>

                               implements List<E> {

     private static final long serialVersionUID = -283967356065247728L;

     final List<? extends E> list; // Here is the wrapped list


     UnmodifiableList(List<? extends E> list) {

         super(list);

         this.list = list;

     }

    ...

}

可以在此處找到用于提取這些代碼的源代碼。


所以回答你的問題:


流使用方法等Collections::unmodifiableList方法創建不可變列表

內部流不會在不同的列表中添加任何內容,因為它ImmutableList只是作為包裝器工作Collection

您還可以查看文檔和來源,以了解這些不可變相關方法和對象的工作原理。


查看完整回答
反對 回復 2022-05-21
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

任何實現都會以某種方式將元素累積到具有某種程度的可變性的結構中,然后返回一個無法修改的列表。

如何完成的細節取決于實現,但這里有幾種可能性:

  • 元素被累積到一個ArrayList中,然后被復制到一個不可變列表中。

  • 元素被累積到一個ArrayList中,并返回一個防止修改的包裝器(例如Collections.unmodifiableList。)由于沒有其他對象引用原始ArrayList的 ,因此結果是不可變的。

  • 這些元素被累積到一些技術上不是列表的結構中,例如原始數組,并且該數組被復制或包裝在不可變的列表對象中。

選擇這些實現中的哪一個取決于Collector您調用的特定對象,例如Collectors.toList()ImmutableList.toImmutableList()。該實現的細節取決于該庫的作者,他們可以使用任何這些策略。


查看完整回答
反對 回復 2022-05-21
  • 2 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號