[JavaSE] Collection framework and List set, javasecollection

Source: Internet
Author: User
Tags addall

[JavaSE] Collection framework and List set, javasecollection
**************************************** ********************************* *** Original article: blog.csdn.net/clark_xu Xu changliang's column**************************************** ********************************

During Event Development, you need to store the objects used in containers with specific data structures. JDK provides Collection framework and map

L Interface Collection includes two subinterfaces: interface List and interface Set

L The List interface includes two implementation classes: ArrayList and javaslist.

L The Set interface includes two implementation classes: HashSet and TreeSet.

The map interface includes two implementation classes: HashMap and TreeMap.

4.1 common methods for the Collection Interface

L int size (); returns the number of contained objects

L boolean isEmpty (); whether to return null

L void clear (); clear the set

L boolean add (E e); add objects to the set

L boolean remove (Object o): delete an Object from the set

L boolean addAll (Collection <? Extends E> c): Adds all the elements in the other set to the collection.

L Boolean removeAll (Collection <?> C). Delete all elements in the same set as the other set.

L Iterator <E> iterator (); returns the Iterator of the set.

4.2 ArrayList and rule list

The List interface is a Collection sub-interface used to define the data structure of a linear table. Two common implementation classes of the List interface are ArrayList and listlist, which implement the List interface using dynamic arrays and linked lists respectively. The method of ArrayList and LinedList is logically the same, but the performance is different. ArrayList is suitable for random access, while ArrayList is more suitable for insertion and deletion;

ArrayList uses a dynamic array to implement the List. You can quickly index the corresponding elements by subscript. However, you need to move more elements when inserting and deleting an element.

ListedList uses a linked List to implement the List. When deleting or inserting a List, you only need to change the "Pointer" of the link;

4.3 common method of List 4.3.1 method of inheriting Collection class by List

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

List list = new ArrayList ();

// Add the object reference to the set

List. add ("one ");

// The List Implementation class overrides the toString method and calls the toString method containing the object at a time.

List. toString ();

// The size method returns the number of objects stored in the current set.

List. size ();

// Clear the set

List. clear ();

List. isEmpty ();

 

// The List ins method implemented by the List Implementation class is used to determine whether the set contains an object. The specific logic is: Call the equals method of the objects contained in the Set in sequence to compare with the objects to be judged. Therefore, the equals method must be properly rewritten for the set objects.

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 elements of another group.

List1.addAll (list2)

// RemoveAll deletes all elements identical to the other set

List1.removeAll (list2)

// RetainAll retains the same elements as the other set and obtains the intersection.

 

The toArray method returns the objects in the set as an array of objects. For example

String [] arry = (String []) list. toArray (new String [] {})

4.3.2 List class definition method

The List class defines a series of methods based on the characteristics of linear tables.

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

The set Method sets the specified Element Based on the subscript parameter and returns the original data of the subscript.

Object obj = list. set (2, "3 ")

With this feature, the I and j elements can be exchanged.

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

The add method with the lower mark is reloaded for the List object. The modified method is used to insert the object to the original list linear table. The object is inserted before the original subscript according to the position of the lower mark, that is, all elements after the corresponding subscript are moved back to storage.

The List object overrides the remove method with the lower mark. After the removal, all elements after the subscript + 1 are moved forward to the storage, and the new List object after the change is returned.

Object obj = list. remove (1 );

Used to retrieve objects. The List provides the indexOf and lastIndexOf methods and also calls the equals method of objects.

4.3.3 List iterator

All Collection implementation classes implement the iterator Iterator method. The method returns an iterator interface type object. Iterator defines three methods:

Boolean hasNext (); determines whether the pointer is followed by an element

E next (); pointer backward shifting, returns the current element

Void remove (); Delete the elements just returned from the original set

Corresponding to the List set, you can traverse through the get method based on the lower mark. While the iterator method is designed for the Collection interface, all classes that implement the Collection interface can use Iterator for iterative traversal.

The common design modes are as follows:

List list = new ArrayList ();

Iterator it = list. iterator ();

While (it. hasNext ()){

Object obj = it. next ();

If ("#". equals (obj) it. remove ();

}

**************************************** ********************************* *** Original article: blog.csdn.net/clark_xu Xu changliang's column**************************************** ********************************

 

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.