2012-05-17 30 views
3

這些方法返回已備份集,因爲在一個集合變化影響的其他集合。[樣的直寫過程]Java中的Backed Collection是什麼?

headSet(e, b)  Returns a subset ending at element e and exclusive of e 

headMap(k, b)  Returns a submap ending at key k and exclusive of key k 

tailSet(e, b)  Returns a subset starting at and inclusive of element e 

tailMap(k, b)  Returns a submap starting at and inclusive of key k 

subSet(s, b, e, b) Returns a subset starting at element s and ending just before element e 

subMap(s, b, e, b) Returns a submap starting at key s and ending just before key e 

那有什麼用Arrays.asList()方法的差異?該方法將數組複製到List中。API說「對返回列表的更改'通過'寫入數組&反過來」。

那麼,它是一個支持的集合嗎?如果是,那麼List接口中的toArray()方法 - 也是一個Backed集合?

是否有其他方法,如Arrays.asList(),它允許通過寫?如何才能知道該方法是否允許通過寫或不通過查看方法簽名?

回答

7

是的,Arrays.asList返回一個由數組支持的列表,因爲它沒有創建副本,但是Collection.toArray創建了一個副本,所以它不支持集合。

您不能分辨方法是否僅從簽名返回由其輸入支持的集合 - 僅從文檔中返回。通常情況下,它使用「備份」,「視圖」或類似的文字進行記錄。有很多例子 - List.subList,例如,Collections.newSetFromMap,還有更多 - 以及第三方庫中的無數例子。

+0

+1你可能想提一下經常使用的'keySet' /'values'和mapMap的headMap' /'subMap' /'tailMap'視圖。 – dasblinkenlight

+0

好吧,公平的說,'headMap','subMap'和'tailMap'已經被OP提及。 –

+0

你是對的,他做到了!我沒有閱讀這個問題中的例子列表,所以我錯過了這三個。 – dasblinkenlight