Use of the Java collection

Source: Internet
Author: User
Tags object object

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.