"Javase" collection Frame and list collection

Source: Internet
Author: User
Tags addall

************************************************************************* * * * Original:blog.csdn.net/clark_xu Xu Changliang's Column************************************************************************

In the development of events, you need to store the objects you use in a container of a particular data structure. JDK provides collection framework Collection and map

l interface Collection consists of two sub-interfaces: interface List and interface Set

the List interface consists of two implementation classes:Arraylist,linkedlist class

l Set Interface consists of two implementation classes:Hashset,treeset class

The map interface consists of two implementation classes:HashMap and TreeMap classes

4.1 CollectionCommon methods of interfaces

l int size (); Returns the number of contained objects

L boolean isEmpty (); Returns whether it is empty

l void Clear (); Emptying the collection

L boolean add (e e); To add an object to a collection

L boolean remove (Object o): Removes objects from the collection

L boolean addall(collection<? extends e> C): Adds all the elements of another collection to the collection

l Boolean RemoveAll (collection<?> c); Delete all elements in the collection that are identical to another collection

l iterator<e> Iterator (); Returns an iterator to a collection

4.2 ArrayListAndLinkedList

The list interface is a sub-interface of Collection that defines a linear table data structure; The two common implementation classes for thelist interface are ArrayList and LinkedList the List interface is implemented by dynamic arrays and linked lists respectively. The ArrayList and linedlist methods can be considered to be exactly the same, but there are some differences in performance. ArrayList is suitable for random access, while LinkedList is more suitable for insertion and deletion;

ArrayList uses a dynamic array to implement List, which can be indexed quickly to the corresponding element, but the insertion and deletion of elements need to move more elements

listedlist The list with a linked list, when deleting or inserting, only need to change the link " pointer " can be implemented;

4.3 ListCommon methods of4.3.1 ListInheritedCollectionMethods of the class

The implementation class of the List interface implements the method of Collection definition

List list=new ArrayList ();

The //add method adds a reference to the object to the collection

List.add ("one");

The //list implementation class overrides the ToString method, calling the ToString method containing the object one time

List.tostring ();

The //size method returns the number of objects that the current collection holds

List.size ();

//clear Method emptying the collection

List.clear ();

List.isEmpty ();

//list implements the contains method of implementing a class to determine whether an object is contained in the collection. The specific logic is that the equals method that invokes the object contained in the collection is compared to the object to be judged, so the collection object needs to be properly overridden by the equals method.

List.contains (P)

The //remove method is also related to the equals method of the object.

List.remove (P);

The //addall method adds all the other collective elements.

List1.addall (LIST2)

//removeall Delete all elements that are identical to another collection

List1.removeall (LIST2)

The //retainall retains the same elements as the other set, taking the intersection.

ToArray method to return the objects in the collection as an array of objects. For example

String[] arry= (string[]) List.toarray (new string[]{})

4.3.2 ListMethod of class definition

The list class defines a series of methods based on the characteristics of a linear table.

The get method obtains the corresponding element according to the subscript, such as list.get (i). toString ();

The set method, based on the subscript parameter, will change the specified element of the element and return to the original data.

Object Obj=list.set (2, " three ")

With this feature, the interchange of element I and element J can be achieved

List.set (J,list.set (I,list.get (j)))

The list object overloads the method with the subscript add , the action of the method is inserted, inserted into the original list linear table, according to the position of the subscript, before the subscript of the original data inserted, All elements after the subscript corresponding element are moved back to storage.

The list object overrides the subscript remove method, and afterremove , all elements after the subscript +1 are moved forward to store, returning the new List object after the change

Object Obj=list.remove (1);

used to retrieve the object,List provides the Indexof,lastindexof method and the equals method that called the object.

4.3.3 ListIterators

All Collection Implementation classes implement the iterator iterator method, which returns an object of the iterator interface type. The Iterator defines three methods:

Boolean Hasnext (); Determine if there are elements behind the pointer

E Next (); The pointer moves back, returning the current element

void Remove (); Delete the element that was just returned in the original collection

For the List collection, traversal can be done by means of a get method based on the subscript, while the iterator method is designed for the Collection interface, and all implements the Collection Interface classes can use Iterator to implement iterative traversal

The common design patterns are:

List list=new ArrayList ();

Iterator It=list.iterator ();

while (It.hasnext ()) {

Object Obj=it.next ();

if ("#". Equals (obj)) it.remove ();

}

************************************************************************* * * * Original:blog.csdn.net/clark_xu Xu Changliang's Column************************************************************************

"Javase" collection Frame and list 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.