The ArrayList collection uses ArrayList arr=new ArrayList ();//add Element Arr.add (new Object ()), Arr.add ("123"); Arr.add ("12313"); Arr.add (New Exception ()); Arr.add (new Date ());//Modify Element Arr.set (2, "123"); for (Iterator Iterator = Arr.iterator (); Iterator.hasnext ();) {Object object = (object) iterator.next (); System.out.println (object);} System.out.println (Arr.size ());//Set Length System.out.println (Arr.indexof ("123"));//Get the position of the element System.out.println ( Arr.contains ("123"));//Whether it contains SYSTEM.OUT.PRINTLN (arr);//Sort//collections.sort (arr);//objects cannot be sorted collections.reverse (arr ); System.out.println (arr); arr.remove (0);//delete element System.out.println (arr) in the collection;//Empty Element arr.clear ();// emptying element System.out.println (arr); System.out.println (Arr.isempty ());//Is null people p1 = new people ("XXX", "x", 10); People p2 = new People ("yyy", "X", 10); People p3 = new People ("zzz", "X",//hashmap set HashMap hs=new HashMap (); Hs.put (1, "QWR"); Hs.put (2, New Date ()); Hs.put ( 3, p1); Hs.put (3, p2);//Repeats the key. Will cover the Hs.put (4, p3); System.out.println (((People) Hs.get (3)). GetName ()); SYSTEM.OUT.PRINTLN (HS); SysteM.out.println (Hs.containskey (1)); System.out.println (Hs.containsvalue ("QWR"));//traverse all values by key System.out.println ("========================"); Set St=hs.keyset (); for (object object:st) {System.out.print (Hs.get (object));} Iterator it= St.iterator (); while (It.hasnext ()) {Object object = (Object) it.next (); System.out.print (Hs.get (object));} Get all Values Collection cc= hs.values (); for (Object o:cc) {System.out.println (o);} The key value is the same as Get set Kv=hs.entryset (); for (Object o:kv) {System.out.println (o);} System.out.println (Hs.size ());//Length Hs.remove (1);//delete hs.clear ();//Empty System.out.println (Hs.isempty ());//is empty// The following two collections are the same as the arrlist usage linkedlist lk = new LinkedList (); Vector v = new vector ();//set of generics arraylist<people> ap=new arraylist<people> (); Ap.add (New People ("Wer", "N", ), Ap.add ("Wrer", "N", "People"), Ap.add (New People ("QW", "n", +)); Iterator Itp=ap.iterator (); while ( Itp.hasnext ()) {System.out.println (((People) Itp.next ()). GetName ());}
Use of the Java collection