These methods return Backed Collection since change in one Collection affects the other Collection.[ kind of write through process ]
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
Then what’s the difference with Arrays.asList() method? The method copies an array into a List.The API says “changes to the returned list ‘write through’ to the array & vice versa”.
So, is it too a Backed Collection? If it’s, then there’s toArray() method in List interface –is that a Backed Collection too?
Is there any other method like Arrays.asList() which allows write through? How can I find out if the method allows write through or not just by seeing the method signature?
Yes,
Arrays.asListreturns a list backed by the array, because it doesn’t make a copy, butCollection.toArraymakes a copy, so it is not backed by the collection.You cannot tell if a method returns a collection backed by its inputs just from the signature — only from the documentation. Usually, it’s documented using the words “backed by,” “view,” or the like. There are many examples —
List.subList, for example,Collections.newSetFromMap, and many more — as well as countless examples in third-party libraries.