Dark Horse Programmer--java Foundation--Collection class

Source: Internet
Author: User

First Lecture Collection Framework

This is the composition of the set frame. Because of the different data structures, there are different sets, also called containers. Here is a brief introduction to the collection class:

First, why does the collection class appear

Object-oriented language is the embodiment of things in the form of objects (all things are 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

Two, the array and the collection class at the same time the container, different points?

Arrays can also store objects, but they are fixed in length, and the collection classes are variable in length. An array can store only basic data, and the collection can be stored as long as the object is (in fact, more convenient).

Three, the characteristics of the collection class

Collections are only used to store objects, are variable in length, and can store different types of objects

Second lecture Collection

Collection is a common interface for collection frames. His two-word interface: List (lists), set (set)

Owning relationship:

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

Set: Elements are unordered, elements cannot be duplicated

I. Common operations in the collection interface 1, adding elements                             Add (Object obj), and the Add method parameter type is object. For easy acceptance of any type 2, delete element                             Remove (object  obj);            Removerall (Another collection): The caller retains only the elements that are not in the other collection                         Clear (): Emptying set 3, judging element        &nb sp;                    contains (Object obj); determine if obj             exists      IsEmpty () to determine whether it is empty 4, get the number, set length           Size (): Number of judgments (a bitWant length ())                                  5, take the intersection                        retainall (another collection): The caller retains only two common elements

Note: A reference to an object is stored in the collection (address)

Second, iterative

1. Overview

An iteration is a way to remove elements from a collection.

This action is taken for the elements of the collection:

is not enough to use a function to describe, need to use a number of functions to reflect, so will take out this action encapsulated as an object to describe. The extraction method is defined inside the set (that is, defined in the inner class), so that the elements inside the collection can be accessed directly when taken out

Then the extraction method is defined as two internal classes

The data structure of each container is different, so the details of the action taken are not the same. But all have the common content: namely judgment and take out. Then it is possible to extract these commonalities.

Then modesty internal classes conform to a rule: iterator. Through an external method: Iterator Iterater (Collection Class), to get the collection of fetched objects.

2. Common Operations for iterations

Iterator Iterator ()

Hasnext () Determines if there is a next element that returns a Boolean

Next (): Take out the next element

Remover (): Remove

The wording of the foreigner:

For(Iterator it=al.iterator (); It.hasnext ();) {

System.out.println (It.next ());

}//become local variable, save space, but not comfortable to watch, not accustomed

Precautions:

* iterators are common in the collection interface, replacing enumeration (enumerations) in the vector class

* Iterators in Next method are automatically down to take elements, to avoid the occurrence of nosuchelementexception

* Iterator in Next method put back type is object, so remember to change type;

Third lecture List

First, List

|--collection

|--list: Elements are ordered and elements can be duplicated. Because it's orderly, the method with the horn is his special method.

| ——— ArrayList: The underlying data structure uses an array structure. Features: Fast query speed, but insert and delete slow. Thread is out of sync

| ——— LinkedList: The underlying uses a linked list data structure. Features: Fast and easy to delete, query slow

| ——— Vector: The bottom layer is the array data structure . Thread synchronization. Replaced by ArrayList. |--set: The elements are unordered, so the elements cannot be duplicated.

|--set: Elements are unordered, elements cannot be duplicated

|--hashset: The underlying data structure-type hash table. Thread is out of sync. The principle of guaranteeing element uniqueness is to determine whether the hashcode value of an element is the same. If the same, you will continue to determine whether the Equals method of the element is ture

|--treeset: You can sort the elements in the set collection. The natural ordering of the default installation letters. The underlying data structure binary tree. The basis for ensuring element uniqueness: CompareTo

Here is the frame of the collection:

Second, the list of unique methods: because it is ordered and with the angle of the method is his special method

1, increase

Add (int index,element)//adding elements after the angle index

AddAll (int index,collection//Adds an element Collection the specified collection after the angle index

1. By deleting

Remover (index)//The element that specifies the angle index

2, change

Set (index,element)//Change the element of the angle index

3. Check

Get (Index)

Sublist (start,end);//Halves

5. Other

Listiterator ();//list-specific iterator, the only iterator that can be added and checked

Int IndexOf (obj);//Gets the position of the first occurrence of the element, the position of the output angle label, and if not, puts back the -1//gets

Iv. linkelist (List of linked lists)

LinkedList: The underlying uses a linked list data structure. Features: Fast and easy to delete, query slow

Unique methods:

1, Increase: AddFirst ();

AddLast ();

2, gets//Gets the element, but does not delete the element. If the element is not there, the Nosuchelementexception

GetFirst ()

GetLast ()

3. Delete//Get elements and remove elements. If not, then nosuchelementexception

Removefirst ()

Removelast ()

After the Jdk1.6, there was an alternative approach

1, increase Offfirst () Offlast ()

2. Get

Gets the element secured for deletion. If not, return null

Peekfirst ();

Peeklast ()

3. By deleting

Gets the element and deletes the element. If not, put back null

Pollfirst ();

Polllast ()

Give me a chestnut.

Lamp: Advanced post-out: a warehouse for a door

Queue: FIFO: two-door warehouse

Give me a chestnut. Remove Duplicates:

Idea: 1, create a new container;

2. Create an iterative removal

3, in the existence of the cycle, the judge is whether the new container exists element, does not exist is the input (note: it. Next () needs to be used two times, so give him a new object class)

4. Return to the new container.

List collection to add objects, note the custom Person () custom in the () class. equals () method

Four, Set.

|--collection

|--list: Elements are ordered and elements can be duplicated. Because it's orderly, the method with the horn is his special method.

| ——— ArrayList: The underlying data structure uses an array structure. Features: Fast query speed, but insert and delete slow. Thread is out of sync

| ——— LinkedList: The underlying uses a linked list data structure. Features: Fast and easy to delete, query slow

| ——— Vector: The bottom layer is the array data structure. Thread synchronization. Replaced by the ArrayList.

|--set: Elements are unordered, so elements cannot be duplicated

|--hashset: The underlying data structure-type hash table. Thread is out of sync.

How does HashSet guarantee the uniqueness of the elements?

is through the elements of the two methods, hashcode and equals to complete

If the hashcode value of the element is the same, it will determine if equals is true

equals is called if the element's hashcode is different

|--treeset: You can sort the elements in the set collection. The natural ordering of the default installation letters. The underlying data structure binary tree. The basis for ensuring element uniqueness: CompareTo

The function of the set set is consistent with the collection. So only iterators Iteractor

so the custom class needs to be carbon hashcode and Equals, better make CompareTo.

Give me a chestnut:

Dark Horse Programmer--java Foundation--Collection class

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.