Set learning notes

Source: Internet
Author: User

I. Concepts

A set is a way to store objects. The collection contains addresses for convenient reference. JDK 1.2 is available.

Ii. Differences between a set and an array

Set is variable length, and array is fixed length.

Arrays can store basic data types. A set can only store objects. A set can store different types of objects.

Collection

1. List: The elements are ordered, repeatable, and indexed.

2. Set: The elements are unordered and cannot be duplicated. They are arranged using hash values.

 

 

 

Iii. Curd

Boolean add (E );

Boolean addall (collection <? Extends E> C );

Void clear (); clear the container

Boolean remove (Object O );

Boolean removeall (collection <?> C );

Boolean contains (Object O );

 

Iv. iterator

The method used to retrieve elements in a set:

Arraylist Al = new arraylist ();
Iterator it = Al. iterator (); // iterator
It. Next (); // Return Value
It. hasnext () // Boolean Type

Each container has its own storage and retrieval. Each container has a different data structure and may have different access methods.

The fetch method is defined inside the set. The fetch method can directly access the elements inside the set. The fetch method is defined as an internal class.

The data structure of each container is different, so the extracted action details are different. However, they all have common content, that is, judgment and retrieval. In this way, these commonalities can be extracted.

 

 

How can I obtain the object retrieved from the set?

By providing external methods, that is, iterator ();

-------------------------------

V. Set

Public interface set extends collection

The Set elements are unordered, and the order of storage and removal is not necessarily the same. The elements cannot be repeated. The functions of the Set set are the same as those of the collection.

1. The underlying data structure of hashset is a hash table. The thread is not synchronous.

Hashcode () and equals ()

If the hashcode of the element is the same, the system checks whether equals is true.

If the hashcode of an element is different, equals is not called.

 

Note: For operations such as determining whether an element exists and deleting it, the dependent methods are the hashcode and equals of the element.

Hashset:

 

[Email protected]

[Email protected]

Tostring (): getclass (). getname () + '@' + integer. tohexstring (hashcode ());

2. treeset

You can sort the elements in the Set set. The thread is not synchronous.

Objects in the treeset must be compared.

The underlying data structure is a binary tree;

The basis for ensuring element uniqueness:

Compareto method, return0

 

The first method of treeset sorting:

The comparable interface must be implemented to override the compareto method.

Natural sequence of Elements

 

Class student implements comparable {

Public int compareto (Object OBJ ){

If (! (OBJ instanceof student )){

Throw new runtimeexception ("not a student object ");

}

Student s = (student) OBJ;

If (this. age> S. Age)

Return 1;

If (this. Age = S. Age)

Return 0;

Return-1;

}

}

Treeset only looks at comparto results

The second sorting method of treeset:

When the element itself does not have the comparison, or the comparison is not required, the set itself needs to have the comparison.

When the integration starts, a comparison method is available.

Compare Method

Overwrite the compare method of Comparator

New INTEGER (s1.getage (). compareto (New INTEGER (s2.getname ()));

 

Vi. List

The list method is unique in this system.

Add (index, element );

Addall (index, collection );

Delete: Remove (INDEX );

Change: Set (index, element );

Query: Get (INDEX)

Sublist (from, );

Listiterator ();

For (INT x = 0; x <Al. Size (); X ++ ){

Sysout ();

}

While (it. hasnext ()){

Sysout ();

}

List set unique iterator

Listiterator it = new Al. listiterator ();

Hasnext = true

Hasprevious = false

Listiterator can add, delete, modify, and query objects during traversal.

--------------------------

Common subclass

1. arraylist: the underlying data structure uses the array structure (jdk1.2)

Features: Fast query speed

Disadvantages: slow increase and deletion (the more elements, the more obvious)

 

2. linked list: the underlying data structure uses a linked list.

Features: insert and delete data blocks

Disadvantage: Slow query speed

Linked List link = new linked list ();

Link. Add ();

 

Unique Method of listing:

Addfirst ();

Addlast ();

 

Getfirst ();

Getlast ();

Get element, but do not delete element,

If there are no elements in the collection, nosuchelementexception will appear;

.

 

Removefirst ();

Removelast ();

Get the element, but the element is deleted,

If there are no elements in the collection, nosuchelementexception will appear;

.

Jdk1.6 appears

Offerfirst ();

Offerlast ();

 

Peekfirst ();

Peeklast ();

Get the element, but the element is not deleted,

If no element exists in the Set, null is returned.

 

Pollfirst ();

Polllast ();

Get the element, but the element is deleted,

If no element exists in the Set, null is returned.

 

While (! Link. isempty ()){

Sop (link. removefirst ());

}

 

Use equals to compare whether the objects are the same

Public Boolean equals (Object OBJ ){

If (! (OBJ instanceof person )){

Return false;

}

Person P = (person) OBJ;

Return this. Name. Equals (P. Name) & this. Age = P. Age;

}

 

3. Vector: the bottom layer is the array data structure (jdk1.0)

The functions of vector and arraylist are the same. The vector is synchronized, And the arraylist thread is not synchronized.

Use arraylist instead of vector whenever possible

-------------------

Enumeration enumeration (jdk1.0)

Enumeration en = V. Elements ();

While (EN. hasmoreelements ()){

Sysout (EN. nextelement ());

}

The enumerated name and method name are too long, so they are replaced by the iterator.

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.