JAVA 13 (Collection Framework)

Source: Internet
Author: User
Tags addall

A collection is a container. Collections and arrays are different: only for storing objects, the collection length is variable, the array can only exist the same type of object, the collection may save any object. Collection |--list: Elements are ordered, elements can be duplicated because the set reminds of an indexed |--arraylist: the underlying data structure uses an array, the query is fast, and the deletion is slightly slower. is not synchronized, easy to out of security risks. Single-threaded, high efficiency. |--linkedlist: The underlying data structure uses the linked list data structure, the query speed is slow, adding and deleting fast |--vetor: The bottom is the array data structure. Victor is synchronous, progress is not |--set: element No, non-repeatable, no index |--hashset: the underlying data structure is a hash table. |--treeset

Collection collection is an interface that contains references to objects that are stored in a collection of ArrayList and LinkedList and vectors   three main classes. The main methods are introduced: Take ArrayList as an example  import java.util.*;p ublic class Test { public static void Main (String adgs[])  {   ArrayList al = new ArrayList ();  ArrayList al2 = new ArrayList ();  al2.add ("PPP");  Al2.add ("KKK"); nbsp;  //1, add element   Al.add ("JAVA1");  al.add ("Java2");  al.add ("Java3");  Al.add (" Java4 ");  //2, get length   SOP (" Size: "+al.size ());  //3, print set   SOP (AL);  //4, Delete elements   Al.remove ("java2"); Use the object's equals to determine if the same   SOP (AL);  //5, empty collection   al.clear ();  SOP (AL);   Al.add (" JAVA1 ");  al.add (" Java2 ");  al.add (" Java3 ");  al.add (" java4 ");  //6, judging elements   SOP (" Whether the JAVA1 exists "+al.contains (" JAVA1 "));  //equals  //7, add collection addall  al.addall (AL2);  sop (AL);  //8, intersect, store in Al   Al.clear ();  al2.clear ();  al.add ("JAVA1");  Al.add ("Java2");  al.add ("Java3");  al.add ("java4");   Al2.add ("PPP");  Al2.add ("KKK");   Al2.add ("Java3");  al2.add ("java4");  Al.retainall (AL2);  sop (AL);  //9, differential set   al/al2  Al.removeall (Al2);  //10,containsall () returns TRUE or false  } public static void SOP (Object O)  {  System.out.println (o); }}  iterator class: InteratorRemove and manipulate elements: unifies the way the collection elements are taken out. Use procedure: Get object Interator it = Collection.iterator (); Method Object It.next ()//Remove Element Boolean it.hasnext ()//Determine if there are elements to remove import ja  va.util.*;p Ublic class Test {public static void main (String adgs[]) {ArrayList Al = new ArrayList ();  1, add Element Al.add ("JAVA1");  Al.add ("Java2");  Al.add ("Java3");   Al.add ("Java4");   /* Iterator it = al.iterator ();//Gets the iterator that is used to remove the element in the collection while (It.hasnext ()) SOP (It.next ()); */for (Iterator it = Al.iterator (); It.hasnext ();)//This notation saves memory sop (It.next ());} public static void sop (Object o) {Sys Tem.out.println (o); }} List  List-specific common methods: The method that can operate the angle is added add (index,element) addall (index,collection) Delete (index) change set (Index,element)//el E: element Check get (index) sublist (from,to) Listiterator () ArrayListImport java.util.*;p Ublic class Test {public static void main (String adgs[]) {ArrayList Al = new ArrayList ();  add Element Al.add ("Java01");  Al.add ("Java02");  Al.add ("java03");   SOP ("meta-set" +al);  Al.add (1, "java09");   SOP (AL);  Deletes the specified position element Al.remove (2);   SOP (AL);  Modify the element Al.set (1, "JJJ");   SOP (AL);   Check SOP ("1:" +al.get (1));  Gets all the elements.  for (int x=0;x<al.size (); x + +) SOP ("Al ()" +al.get (x));   Get the position of the object via IndexOf SOP (Al.indexof ("java03"));    List sub = al.sublist (1, 3);    SOP (sub); } public static void sop (Object o) {System.out.println (o);}} List iteratorsImport java.util.*;p Ublic class Test {public static void main (String adgs[]) {ArrayList Al = new ArrayList ();  Al.add ("Java01");  Al.add ("Java02");   Al.add ("java03");   Iterator it = Al.iterator ();      while (It.hasnext ()) {Object obj = It.next ();     if (Obj.equals ("JAVA02")) It.remove (); The JAVA02 is removed from the collection.  However, the Al.add () operation is not supported//the exception SOP (obj) is thrown; } SOP (AL); } public static void sop (Object o) {System.out.println (o);}} So there's the list iterator: LinkedList  Unique method: AddFirst (obj);//Add objaddlast (obj) at the very beginning of ll; GetFirst (); , tail getlast (); Removefirst ();//Gets the element, but the element is deleted. If there are no elements, an exception occurs. Removelast ();   ArrayList Practice  The core of removing duplicate objects is to override the Equals method of the object to note the application of type-conversion polymorphism. Set   set Interface: The element is not required and cannot be duplicated. The function of set is to establish the object when the collection is consistent with the HashSet

JAVA 13 (Collection Framework)

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.