Java duration-Java set

Source: Internet
Author: User

The Java set is intended for arrays.

The array size is fixed, and the same array can only store data of the same type (basic type/reference type)

A Java set can store a set of data with an unfixed number of operations.

All Java collections are located in the Java. util package!

Java collections can only store reference data types, but cannot store basic data types.

Java collections are mainly divided into three types:

Set)

List)

Map)

Collection Interface

Collection is the most basic collection interface and declares a general method applicable to Java collections (including set and list only.

Both set and list inherit conllection, while map does not

Method of the collection interface:

Boolean add (Object O): Add an object reference to the set

Void clear (): deletes all objects in the set, that is, they no longer hold references to these objects.

Boolean isempty (): determines whether the set is empty.

Boolean contains (Object O): determines whether a specified object is referenced in a collection.

Iterartor iterator (): returns an iterator object that can be used to traverse elements in the set.

Boolean remove (Object O): deletes an object reference from the set.

Int size (): returns the number of elements in the set.

Object [] toarray (): returns an array containing all elements in the set.

The iterator () and toarray () methods are used for all elements of the set. The former returns an iterator object, and the latter returns

An array containing all elements in the collection.

The iterator interface declares the following methods:

Hasnext (): determines whether the elements in the set have been traversed. If not, true is returned.

Next (): returns the next element.

Remove (): deletes an element returned by the next () method from the set.

Set ):

Set is the simplest set. Objects in the set are not sorted in a specific way and there are no repeated objects.

The Set interface mainly implements two implementation classes:

Hashset: The hashset class follows the hashAlgorithmTo access objects in a set.

Treeset: The treeset class implements the sortedset interface to sort objects in the set.

Set usage:

It stores object references without repeated objects.

Set set = new hashset ();

String S1 = new string ("hello ");

String S2 = S1;

String S3 = new string ("world ");

Set. Add (S1 );

Set. Add (S2 );

Set. Add (S3 );

System. Out. println (set. Size (); // The number of objects in the print set is 2.

How does the add () method of set determine whether the object has been stored in the collection?

Boolean isexists = false;

Iterator = set. iterator ();

While (it. hasnext ()){

String oldstr = it. Next ();

If (newstr. Equals (oldstr )){

Isexists = true;

}

}

List ):

The feature of list is that its elements are stored linearly, and repeated objects can be stored in the collection.

The main implementation classes of the List interface include:

Arraylist (): indicates that the length of an array can be changed. Allows Random Access to elements, and inserts and

It is slow to delete elements.

Linked List (): uses the Linked List Data Structure in implementation. Fast insertion and deletion, and slow access.

For random access to a list, only random elements are retrieved at a specific location.

The get (INT index) method of list puts back the object at the index location specified by the index parameter in the set. The subscript starts from "0.

The two most basic methods to retrieve all objects in a set:

1: Use the for loop and get () methods:

For (INT I = 0; I <list. Size (); I ++ ){

System. Out. println (list. Get (I ));

}

2: Use iterator ):

Iterator it = List. iterator ();

While (it. hashnext ){

System. Out. println (it. Next );

}

Map ):

Map is a set that maps key objects and value objects. Each element of map contains a pair of key objects and value objects.

Map does not inherit from the collection Interface

When retrieving elements from a map set, the corresponding value object is returned as long as the key object is given.

Common map methods:

1. add and delete operations:

Object put (Object key, object value): Add elements to a collection

Object remove (Object key): Delete key-related elements

Void putall (map t): Add all elements from a specific image to the image

Void clear ():Delete all mappings from the image

2 query operations:

Object get (Object key): Get and keywordKeyRelated Value

Duplicate key objects are not allowed in the map set. That is to say, the comparison results of any two key objects through the equals () method are false.

However, you can map any number of keys to the same value object.

 

Conllections: collections class

Conllections providesJavaA set of practical static methods

 

Summary:

JavaThe basic usage of the set is summarized. The above are the most commonJavaFor more information, see the JDK help documentation. There are many map applications. Specifically, conllections provides many list/map practical methods, it is very useful for normal development.

The next meeting will be continuously modified!
 

 

Boolean containskey (Object key):Determines whether a keyword exists in the image.Key

 

 

Boolean containsvalue (object value):Determines whether a value exists in the image.Value

 

<O: P> </O: P>

Int size ():Returns the number of mappings in the current image.

<O: P> </O: P><O: P> </O: P>

Boolean isempty (): Determines whether there is any ing in the image.<O: P> </O: P>

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.