COLLECTION interface analysis

xiaoxiao2021-03-06  56

A lot of collective classes (also called aggregation or containers) are defined in Java, and Collection is the parent interface of all collection classes. Thecollection interface defines 15 methods. There is no constant definition, and these methods are briefly explained here. 1.Size () gets the elements in the collection. Number, if the number is greater than Integer.max_Value, return Integer.max_Value.2.isempty () If any element is not included, return True3.Contains (Object O) If you contain a specified element, return true4.ITerator () Return iterator 5.Toarray () Returns an array Object [] 6.toarray (Object [] a) Returns an array object [], if a can contain elements in the collection, return A, otherwise generate a new array. 7.Add (Object O Add an element, successfully returns True8.Remove (Object O) Delete a data, successfully returns true9.addall (Collection C) Add all elements included in the collection C, if the collection changes, return True10.Removeall (Collection C) Deleting all elements contained in the collection C, if the collection changes, returning True11.Retainal (Collection C) Delete is not included in all elements in the collection C, if the collection changes, return True12.ContainSall (Collection C) If included All elements in c, return True13.clear () Clear collection, ISEMPTY should be determined by TRUE14.EQUALS (Object C) to return a hash value for TRUE14.EQUALS (Object C).

There are a few points here: 1. If the collection changes, if the collection changes, it returns True. This definition is unstrusted. For a single record (add, remove) If the specified object is added or deleted, return For the add operation, if false indicates that the same object has the same object and does not allow duplicate. For the REMOVE operation, if it returns false, there is no object in the collection. For bulk operations, it is even more unsuspetent. If you call AddAll (Collection c), there are 10 records in C, and the results only add a record and return TRUE, but it is definitely already containing other elements in the collection, otherwise it should throw an exception, that is, if returned to True, Containsall (Collection c) What should be true for True.2.toArray (Object [] a) method? Because the toarray () method needs to regenerate an array, although this array structure is in the Collection, it must be generated a new one. Array, not directly returning an array, such as Collection is not safe. Toarray (Object [] a) utilizes an existing A array (if you can accommodate) without need to reassign space. 3.equals () and The hashcode () method is a bit redundant because these two methods have been defined in Object, and all classes have inherited Object, so it seems to be more, but it is not very uncommon. 4. In the Java class library, there is no direct inheritance of the class, They are all sub-interfaces inherited, such as List, SET, MAP, etc., and they don't intersect, that is, it is not only LIST and SET.

转载请注明原文地址:https://www.9cbs.com/read-118623.html

New Post(0)