The "destructive" methods contained in this interface, that is, the methods that modify the map on which they operate, are specified to throw UnsupportedOperationException if this map does not support the operation.
-
2What interface? Which map?– SilverNakMar 16, 2017 at 7:28
-
Do you know what mutable and immutable operations are? sounds similar– vikingsteveMar 16, 2017 at 7:47
-
Thanks SilverNak ,that is Map interface– SubbbuMar 16, 2017 at 9:03
-
yes,i know -SilverNak , thanks– SubbbuMar 16, 2017 at 9:07
2 Answers
A simple explanation would be that a destructive method is a method that alters the attribute of an object. A non-destructive method you do no alter the attribute of an object.
In your case, destructive would mean that the contents of the Map would be modified and non-destructive would be a way to achieve your goal without modifying the contents.
If you take another example, let's say you have ArrayList object list1
and list2
and you want elements in both of them in same list.
list1.addAll(list2);
This will do it , but you are changing number of elements in list1
. So this is destructive form of doing it.You are destructing list1
by appending more elements to it.
List<Type> list3 = new ArrayList<Type>(list1);
list3.addAll(list2);
In this case, you are satisfying your goal to put elements in list1
and list2
in the same bucket but without changing any one of them by creating a new list called list3
and using it to put all elements in your lists.This is non-destructive way of doing it because you are not changing list1
here, it remains the same all time.
For others: the question is about the java.util.Map
interface, this sentence is in its documentation.
Destructive and non-destructive methods are not a general Java term, it's just in the scope of this interface.
As it says in the quoted sentence, destructive methods are ones that would modify the contents of the Map, that would be things like put
, clear
, or remove
. Non-destructive methods are ones that only perform read operations, like get
, containsKey
, or containsValue
.