Dark Horse Programmer-----Collection Framework Class (i)

Source: Internet
Author: User

Dark Horse Programmer-----Collection Framework Class (i)

1.1 Why does the collection class appear?

Object-oriented language is the embodiment of things in the form of objects, so in order to facilitate the operation of multiple objects, the object is stored, the collection is the most common way to store objects.

1.2 Arrays and collections are similar to containers, what is the difference?

Arrays can also store objects, but they are fixed in length, and the set length is variable. The base data type can be stored in an array, and the collection can store only objects.

1.3 Characteristics of the collection class

Collection is only used to store objects

The set length is variable

Collections can store different types of objects.

1.4 Composition and Classification of the Integrated Framework:

2.1 Collection Interface:

Collection defines the common functionality of the collection framework.
1, adding
Add (e);
AddAll (collection);

2, delete
Remove (e);
RemoveAll (collection);
Clear ();

3, Judge.
Contains (e);
IsEmpty ();

4, get
Iterator ();
Size ();

5, gets the intersection.
Retainall ();

6, set of set variables.
ToArray ();

The parameter type of the 1,add method is object. To make it easy to receive any type of object.

2, the object's reference (address) is stored in the collection

2.1.2 Iterators: How collection frame elements are removed

What is an iterator?
is actually the way the collection is taken out of the element.
Like a gripper in a doll game machine.

Iterators are fetch methods that directly access the elements in the collection.
So the iterator is described in the form of an inner class.
Gets the object of the inner class through the container's iterator () method.

Example 1:

1 classCollectiondemo2 {3      Public Static voidMain (string[] args)4     {5         6 method_get ();7     }8      Public Static voidMethod_get ()9     {TenArrayList Al =NewArrayList (); One  A         //1, add the element.  -Al.add ("Java01");//Add (Object obj); -Al.add ("Java02"); theAl.add ("Java03"); -Al.add ("Java04"); -  -         /* + Iterator it = Al.iterator ();//Gets an iterator that is used to remove an element from the collection.  -  + While (It.hasnext ()) A         { at SOP (It.next ()); -         } -         */ -  -          for(Iterator it =al.iterator (); It.hasnext ();) -         { in SOP (It.next ()); -         } to     } +  -  the      Public Static voidmethod_2 () *     { $ArrayList Al1 =NewArrayList ();Panax Notoginseng  -Al1.add ("Java01"); theAl1.add ("Java02"); +Al1.add ("Java03"); AAl1.add ("Java04"); theArrayList Al2 =NewArrayList (); +  -Al2.add ("Java03"); $Al2.add ("Java04"); $Al2.add ("Java05"); -Al2.add ("Java06"); -  the          -         //Al1.retainall (AL2);//Al1, only the same elements in the Al2 are preserved, and if Al1 and Al2 do not intersect, the print result is [], emptyWuyiAl1.removeall (AL2);//to intersect, all only retains elements that are not the same as the Al2 the  -SOP ("Al1:" +al1); WuSOP ("Al2:" +al2); -  About  $  -  -     } -  A      Public Static voidBase_method () +     { the         //creates a collection container. Subclasses that use the collection interface. ArrayList -ArrayList Al =NewArrayList (); $  the         //1, add the element.  theAl.add ("Java01");//Add (Object obj); theAl.add ("Java02"); theAl.add ("Java03"); -Al.add ("Java04"); in  the         //prints the original collection.  theSOP ("Original set:" +al); About  the  the         //3. Delete the element.  the         //al.remove ("java02"); +         //al.clear ();//empties the collection.  -  the Bayi         //4, judging elements.  theSOP ("JAVA03 is present:" +al.contains ("Java03"))); theSOP ("Is the collection empty? "+al.isempty ()); -  -  the         //2, get the number. The collection length.  theSOP ("Size:" +al.size ()); the  the         //prints the changed collection.  - SOP (AL); the  the     } the 94      Public Static voidsop (Object obj) the     { the System.out.println (obj); the     }98}

Collection

|--list: Elements are ordered and elements can be duplicated. Because the collection system has an index.

|--arraylist: The underlying data structure uses an array structure. Features: Fast query speed. But additions and deletions are slightly slower. Thread is out of sync.

|--linkedlist: A linked list data structure used at the bottom. Features: Deletion speed quickly, query slightly slow. Thread is out of sync. (I only know the next man, I do not know others, like chain to connect up)

|--vector: The bottom layer is the array data structure. Thread synchronization. Replaced by the ArrayList. Because the efficiency is low.

|--set: Elements are unordered and elements cannot be duplicated. 、

|--hashset: The underlying data structure is a hash table. is not thread-safe. Different steps.
How does HashSet guarantee the uniqueness of the elements?
is done through the elements of the two methods, hashcode and equals.
If the hashcode value of the element is the same, the equals is not determined to be true.
If an element has a different hashcode value, equals is not called.

Note that for actions such as determining whether an element exists, as well as deletions, the dependent method is the hashcode of the element and the Equals method.


|--treeset: You can sort the elements in the set collection.

        The underlying data structure is a two-fork tree.

The basis for ensuring element uniqueness:
CompareTo method return 0.

TreeSet the first way to sort: to make the elements themselves comparable.
The element needs to implement the comparable interface, overriding the CompareTo method.
It also becomes the natural order of the elements, or is called the default order.

The second sort of treeset.
When the elements themselves are not comparable, or have the comparative is not required.
At this point you need to make the collection itself comparable.
When the collection is initialized, there is a comparison method.


List:
Unique method. All the methods that can operate the angle mark are the methods peculiar to the system.

Increase
Add (index,);
AddAll (index,collection);

By deleting
Remove (index);

Change
Set (index,element);
Check (GET)
Get (Index):
Sublist (from,to);
Listiterator ();
int indexOf (obj): Gets the position of the specified element.
Listiterator Listiterator ();

An iterator that is unique to the list collection. Listiterator is a sub-interface of iterator.

In an iteration, you cannot manipulate the elements in the collection through the methods of the collection object.
Because the concurrentmodificationexception exception occurs.

So, in an iterator, you can only use an iterator-sparing element, but the iterator method is limited,
Only the elements can be judged, taken out, removed from the operation,
If you want other operations such as add, modify, and so on, you need to use its sub-interface, Listiterator.

This interface can only be obtained through the Listiterator method of the list collection.

Dark Horse Programmer-----Collection Framework Class (i)

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.