Java learning notes Collection

zhaozj2021-02-16  47

Time: 2003/12/24 Content: Java.util class

Java Collection has unified management for Dictionary, Vector, Stack, and Properties. The interface to support the class set is as follows: CollectionListSortedSet Comparator Defines how two objects are compared to the object ListIterator enumeration class in the Iterator enumeration class.

Collection interface (omission of common methods) Boolean add (Object obj) Add an Object element Boolean addall (Collection C) Boolean Contains (Object Obj) Judging whether the OBJ is an element (belonging to) Boolean ContainSall (Collection C) judgment C is a subset of the call class set (included) Boolean Equals (Collection C) determines whether c is the Hash Code Iterator Iterator () returned to the call class set, etc., is the Iterator Iterator () returned to the called class set. Boolean RemoveAll (Collection C) Remove the Elements (Differential C) included in all C in the call class (Collection C) to the Element (补 补) Object [] Toarray () returns from the CLET [] TOARRAY () return class set from the call class. Array of elements Composed Void Clear () Boolean ISempty () int size ()

The class set contains an Add (Object Obj) method, so you can include any Object data, but you can't store data from int, char, double. You can implement the following method: arraylist a = new arraylist (); A.Add (New Integer (1)); A.Add (New Integer (2)); ......

UnsupportedOperationException can be triggered when the class set cannot be modified. Attempting to add an incompatible object to a class set when it may cause ClassCastException.

List interface (inherited from Collection) Void Add (int index, object obj) Insertion points will be moved back to Boolean Addall (int index, collection c) If the call list changes, return true Otherwise, returning falseObject get (int index) int indexof (object obj) Returns the index in the list, there is no return -1int lastindexof (object obj) returns the subscript of the last instance of the OBJ, there is no return -1listiterator Listiterator () Listiterator ListIterator (int index) Returns the Itex Object Set (int index, object obj) of Index to modify the value at the list Index from Start to END-1

SET interface (derived from Collection, no new method) SET does not allow repeated elements. To call the Add (Object Obj) method, if the OBJ already exists, false will be returned.

SortedSet Interface Comparator Comparator () Returns the comparison function of the sort collection, if the collection uses the natural order, return nullobject first () Returns the first element of the sort collection SortedSet Headset (Object End) Returns a SortedSetObject that contains less than END Elements. Last () Returns the last element Sorted Subset (Object Start, Object End) of the Call Sort Collection Includes from Start to End-1 SortedSet TailSet (Object Start). Returns the Element ArrayList extension the AstractList class that is greater than equal to Start and performs a list interface. ArrayList supports an array of dynamic lengths. LinkList extends AbstractSequentialList, performs a list interface. Provide a list of connections. Hashset extension AbstractSet implements the set interface, and the elements have no order. Constant level basic operations are provided for large collections. Treeset uses a tree to store the SET, and the object is stored in an ascending order. Access and retrieval very fast.

Iterator implements the Iterator interface or the Listiterator interface. Iterator interface Boolean Hasnext () Object next () If no next element, the NOSUCHELEMENTEXCEPTION is triggered. Void remove () Deletes the current element, if you try to call the remove () method after calling the next () method, trigger an IllegalStateException exception.

Listiterator interface Void Add (Object OBJ) Call the next () method to return the next element before inserting an element into the current element. Boolean Hasnext () Boolean Hasprevious () Object Next () If there is no trigger nosuchementexceptionint next, if there is no return list, the size of the return list void () Void set (Object obj) Modify the current element

Public void test1 () {arraylist al = new arraylist (); for (int i = 1; i <10; i ) {al.add ("arraylist element:" i);} iterator itr = al.listiterator () WHILE (ITR.hasNext ()) {Object Obj = ITr.Next (); System.out.Println (OBJ);}}

Public void test2 () {hasHset HS = new hashset (); system.out.println ("hashset"); for (int i = 1; i <10; i ) {hs.add ("Hashset Element:" i }} Itrator itr = hs.Itemrator (); while (itr.hasnext ()) {Object obj = iTr.next (); system.out.println (});}}

Public void test3 () {treeset ts = new treset (); system.out.println ("treset"); for (INT i = 1; i <10; i ) {ts.add ("Treeset Element:" i );} Iterator itr = ts.iterator (); while (itr.hasnext ()) {Object obj = itr.next (); system.out.println (}} public void test4 ()}} public void test4 ()}} public void test4 ()}} New hashMap (); for (int i = 0; i <10; i ) {hm.put ("item" i, "value" i);}

Set set = hm.entryset (); item ip (); while (ipr.hasnext ()) {map.entry me = (map.entry) ITr.next (); system.out.println (ME .getKey () "; me.getValue ());

HM.Put ("item5", "modifyed value"); system.out.println (hm.get ("item5");

Set = hm.entryset (); it (appl.text ()) {map.entry me = (map.entry) iTr.next (); system.out.println (Me.GetKey () "; me.getvalue ());

}

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

New Post(0)