Java container

The container collection interface defines methods to access a set of objects, Its sub interfaces set and list respectively define the storage mode. The data objects in set have no order and cannot be repeated. The data objects in HashSet · list have order and can be repeated. LinkedList (linked list at the bottom) ArrayList (array at the bottom) · array reads faster and slower · linked reads faster and slower · hash. The map interface between them defines the storage "key value mapping pair" Method HashMap ① the method int size() Boolean isempty() void clear() Boolean contains (object element) defined in the collection interface / / whether the element element Boolean add (object element) Boolean remove (object element) iterator iterator() is included; Boolean containsall (collection C) / / whether all the elements in collection C are included. Boolean addall (collection C) Boolean removeAll (collection C) / / remove all the elements in collection C. Boolean retainAll (collection C) / / the intersection with another collection. Object [] toarray() ② the iterator interface. In order to uniformly traverse the methods in the collection, ·All container classes that implement the collection interface have an iterator method to return an object that implements the iterator interface. The iterator object is called an iterator, The iterator interface defines the following methods for traversing elements in the container. Boolean hasnext() / / judge whether there is an element on the right side of the cursor. Object next() / / return the element on the right side of the cursor and move the cursor to the next position. Void remove() / / delete the element on the left side of the cursor, After executing next, the operation is only executed once. Supplement: enhanced for loop defect: · array: it is not convenient to access subscript values · set: compared with using iterator, it is not convenient to delete the content summary in the set: in addition to simple traversal and reading out the content, int [] arr = {1,2,3,4,5}; for(int i : arr){ System.out.println(i); } Collection c = new ArrayList(); c.add(); c.add(); c.add(); for(Object o : c){ System.out.println(o); } ③ Set interface · sub interface of collection. Set interface does not provide additional methods, but the elements in the container class implementing set interface are not sequential and cannot be repeated · set container can correspond to the concept of "set" in mathematics · set container classes provided in J2SDK API include HashSet, TreeSet, etc. ④ list interface · list interface is a sub interface of collection, The elements in the container class that implements the list interface are sequential and repeatable. The elements in the list container correspond to an integer serial number to record their position in the container. The elements in the container can be accessed according to the serial number. The list container classes provided by J2SDK include ArrayList, LinkedList, etc. List container common algorithms · void sort (list) sort the elements in the list container · void shuffle (list) sort the objects in the list container randomly · void reverse (list) sort the objects in the list container in reverse order · void fil (list, object) Rewrite the whole list container with a specific object · coid copy (list DeST, list SRC) Copy the contents of SRC list container to dest list container · int binarysearch (list, object) for sequential list containers, use the half search method to find specific objects ⑤ comparable interface · all classes that can "sort" implement this method, · there is only one method, public int CompareTo (object object); Returning 0 means this = = obj returning a positive number means this > obj returning a negative number means this < obj ⑥ map interface · classes implementing map interface are used to store key value pairs · implementation classes of map interface include HashMap and treemap · key value pairs stored in map classes are identified by keys, Therefore, the key value cannot be repeated. Object put (object key, object value) object get (object key) object remove (object key) Boolean containskey (object key) Boolean containsvalue (object value) int size () Boolean isempty () void putall (map T) void clear() auto package auto - @ r_ 478_ 2419@ing / un@R_478_2419 @Ing · automatic packaging and unpacking at the time of verification · automatic conversion of objects to basic type generic generic cause: · jdk1 4. Before, the type was not clear: · the types loaded into the collection were treated as objects, thus losing their actual type · transformation is often required when taking them out of the collection, which is inefficient, Error prone solutions · define the types of objects in the collection when defining the collection · instances: · you can specify them when redefining the collection · you can also specify list < string > C = ArrayList < string > () with iterator during the loop; Iterator
it = c2. iterator(); Benefits · enhance the readability and stability of the program supplement: object is the parent class of all classes. Some methods tostring() method · public string tostring() describe the relevant information of the current object · when connecting string with other types of data, (for example, system.out.println ("info" + person)) will automatically call the tostring() method of the object class. You can override the tostring() method hashcode() method hash code equals() method in user-defined types as needed, which is equivalent to "= =" so most classes override the equals() method public Boolean equals (object obj) to compare whether objects are equal, Rewrite public Boolean equals (object obj) {if (obj = = null) return false; else {if (obj instanceof class) {/ / class to judge whether obj belongs to class class C = (class) obj; if (c.color = = this. Color & &...) {/ / all attributes are the same return true;}}}} Summary: 1136 · a graph collection map / | set list HashMap / / \ HashSet ArrayList LinkedList · a class · collections · three knowledge points · for · generic · auto - @ r_478_ 2419@ing / un@R_478_2419 @Ing · six interfaces · collegeton · set · list · map · iterator · comparable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>