Cloneable :
public interface Cloneable
--> classes that implement this interface should override Object.clone(protected)
with a public method.
(Cant reduce the visibility of the method inherited).
--> This interface does not contain the clone method.Therefore, it is not possible
to clone an object merely by virtue of the fact that it implements this interface.
Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.
protected Object clone() throws CloneNotSupportedException
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by
super.clone
before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone
need to be modified. The method
clone
for class Object
performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable
, then a CloneNotSupportedException
is thrown. Note that all arrays are considered to implement the interface Cloneable
and that the return type of the clone
method of an array type T[]
is T[]
where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation. The class
Object
does not itself implement the interface Cloneable
, so calling the clone
method on an object whose class is Object
will result in throwing an exception at run time.Comparable:
public interface Comparable<T>
Lists (and arrays) of objects that implement this interface can be sorted automatically
by Collections.sort
(and Arrays.sort
). Objects that implement this interface can be used as
keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.int compareTo(T o)
Compares this object with the specified object for order.
Returns a negative integer, zero, or a positive integer as this object is less than,
equal to, or greater than the specified object.
Throws:
NullPointerException
- if the specified object is null
ClassCastException
- if the specified object's type prevents it from being compared to this object.
Comparator :
Interface Comparator<T>A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as
Collections.sort
or Arrays.sort
) to allow precise control over the sort order.Comparators can also be used to control the order of certain data structures (such as
sorted sets
or sorted maps
), or to provide an ordering for collections of objects that don't have a natural ordering
Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.
For example, suppose one adds two elements
a
and b
such that (a.equals(b) && c.compare(a, b) != 0)
to an empty TreeSet
with comparator c
. The second add
operation will return true (and the size of the tree set will increase) because a
and b
are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add
method.Unlike
Comparable
, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation.int compare(T o1,T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Throws:
NullPointerException
- if an argument is null and this comparator does not permit null arguments
ClassCastException
- if the arguments' types prevent them from being compared by this comparator.
No comments:
Post a Comment