2012-05-17 59 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