A collection framework of Java Basics (i)

Source: Internet
Author: User

Related definitions of the collection framework

Collection : A container that stores objects.
Framework : A collection of class libraries.
Collection Framework : A unified framework for representing and manipulating collections, with related interfaces, implementation classes, and algorithms that help programmers complete programming.

Function of the collection:

    • Organization of data within a class
    • Simple and fast search for large numbers of entries
    • There is a collection interface that provides a series of ordered elements that can be quickly inserted or deleted in the middle of a sequence
    • There is a collection interface that provides a mapping relationship that can be used to quickly find the corresponding unique object by keyword (key), which can be any type

The difference between a collection and an array:

    • The set length is variable and the array length is fixed.
    • Only objects can be stored in the collection, and the base data type cannot be stored, and both of the arrays can be stored
    • The objects stored in the collection can be of any type, whereas an array can only store objects of the same type or data of the same base type

Related instructions:

    • Any type of object can be stored in the collection because the object is in the set of objects, so be aware of the type conversion when you remove it, or you can add generics to specify that the collection can only mount objects of a particular type.
    • Note that only objects can be stored in the collection, and when we add basic data types such as shaping in the collection, it is possible to compile successfully because the automatic boxing unboxing mechanism is added after JDK1.5, the int type is automatically boxed into an integer type, and so on.
Composition of the Set frame

Because many of the class functions in the collection framework are similar, a large number of interfaces have been designed to standardize the class, most importantly the two root interfaces, the collection interface and the map interface
The collection interface also has three important sub-interfaces: List interface, queue interface, set interface, these three sub-interfaces correspond to three important implementation classes: ArrayList, linklist, HashSet
The map interface corresponds to an important implementation class: HashMap

Collection interface

Collection interface in some of the common methods, mainly to delete and change:

To Add a method:
-Boolean Add (Object e)--only one element can be added at a time
-Boolean AddAll (collection< extends e> c)--adds all the elements in a parameter container to the current container

Delete method:
-Boolean Remove (object e) To delete a specified object
-Boolean RemoveAll (collection<> c) deletes the same element in the specified Collection and in this Collection
-Void Clear (): empties the elements in the collection directly

Judging Method:
-whether Boolean contains (Object) contains the specified element
-Boolean Containsall (collection<> c) contains the elements in the specified container
-Boolean Imempty (): whether there are elements

get the number of elements:
-int size (), gets the number of elements

To take the intersection:
-Boolean Retainall (collection<> C) retains and specifies the same elements in the Collection collection, and the different elements are deleted

to transfer a collection to an array:
-object[] ToArray ()

method of extracting elements (key Mastery)
-Iterator Iterator ()

Iterator iterator

An iterator is a design pattern that is an object that takes the elements of a collection, built into a container, and can be done with the iterator () method, which returns a iterator type object.
The removal method in each container is encapsulated. and external exposure. In this way, no matter what the container or data structure, as long as the internal extraction method to implement the iterator interface, you can remove the elements of these containers through the interface.
The appearance of this method separates the container from the data structure of the container and reduces the coupling. The extraction method is defined in the container because it accesses the elements in the container directly and relies on the specific data structure. The iterator interface is implemented through an inner class.

The usage is relatively simple:
-Using method iterator () requires the container to return a iterator. The first time you call Iterator's next () method, it returns the first element of a sequence. Note: The iterator () method is an Java.lang.Iterable interface that is inherited by collection.
-Use Next () to get the next element in the sequence.
-Use Hasnext () to check if there are elements in the sequence.
-use Remove () to delete the newly returned element of the iterator.

Example:

Import Java. Util. ArrayList;Import Java. Util. Collection;Import Java. Util. Iterator;public class Collectiontext {public static void main (string[] args) {collection<string> Collection = new Arraylist<string> ();Collection. Add("a");Collection. Add("B");Collection. Add("C");Collection. Add("D");Collection. Add("E");iterator<string> Iterator = Collection. Iterator();while (iterator. Hasnext()) {System. out. println(Iterator. Next());}    }}
Mastery of skill

Specify the suffix of the specific collection object name:
-If the suffix is list, it belongs to the list system, which is usually non-synchronous.
-If the suffix is set, all of the set systems are usually non-synchronous.
-Other subclass objects in these systems, suffixes are not owned by the interface name, and are generally synchronous. This is common in common subclass objects.

Clear data structure:
-For the jdk1.2 version of the subclass object. The suffix name is the system to which it belongs. The prefix name is the name of the data structure.
- ArrayList: When you see an array, it is the array structure; query fast
- LinkedList: When you see link, you need to be clear about the list structure, you should think of the add get remove and first last combination method. Quick and delete
- HashSet: to see the hash, it is necessary to be clear hash table, query fast, and the stored objects are unique. It comes to mind that elements must override the Hashcode method and the Equals method.
- TreeSet: To see the tree, it is necessary to be clear is a two-fork tree, you can sort the elements, you need to think of two sorts: natural sort: Comparable interface, covering CompareTo (one parameter) Java.lang. Comparison sort: Comparator interface, covering compare (two parameters): Java.util. The basis of judging element uniqueness is the return result of the comparison method return 0;

Reference article:
http://blog.csdn.net/watermusicyes/article/details/7985381
Http://www.cnblogs.com/amboyna/archive/2007/09/25/904804.html
Http://developer.51cto.com/art/201107/274951.htm

A collection framework of Java Basics (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.