Java Foundation--Set (i)--collection system, collection collection

Source: Internet
Author: User

I. Overview of the collection

Java is an object-oriented language, and if we are to operate on multiple objects, we must store multiple objects. and the array length is fixed, it can't meet the requirement of change. So, Java provides a collection.

Characteristics

1. Length can vary

2. Only objects can be stored

3. Multiple types of objects can be stored

Difference from array

tbody>

 

array

set

< P style= "Text-align:center" > length

fixed

variable

storage elements

basic type/reference type

reference type

element type consistency

must be consistent

can be inconsistent

Second, set system

A collection is a container that can store multiple elements, but because of the different data structures, Java provides a variety of collection classes. The function of the CPC in the collection class is extracted continuously, and finally the set architecture is formed.

Data structure: How to store

Diagram of collection classes in Java

Set System Map ( Drag the picture with the mouse to see a larger image or right-click on the picture to open the picture in a new tab page (I))


Third, Collection

Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Some collection allow the same elements while others do not. Some can be sorted and others are not, thus deriving two sub-class interfaces list and set.

Collection Basic Functions

A: Add Features

Booleanadd (Object obj): Adds an element to the collection

Booleanaddall (Collection C): Adds an element of a collection to the collection.

B: Delete Feature

Voidclear (): Deletes all elements in the collection.

Booleanremove (Object obj): Removes the specified element from the collection

Booleanremoveall (Collection C): Removes a specified collection element from the collection.

C: Judging function

Booleanisempty (): Determines whether the collection is empty.

Booleancontains (Object obj): Determines whether the specified element exists in the collection.

Booleancontainsall (Collection C): Determines whether an element in the specified collection exists in the collection.

D: Traversal function

Iterator Iterator (): It is used to get every element in the collection.

E: Length function

Intsize (): Gets the number of elements in the collection

F: Intersection function

Booleanretainall (Collection C): Determines whether two sets have the same element.???

G: Convert set to Array

Object[]toarray (): Turns the collection into an array.

List Interface

The collection elements under the list interface are stored in order and can be duplicated.

Unique features of list

A: Add Features

Voidadd (int index, Object obj): add element at specified position

B: Delete Feature

Objectremove (int index): Deletes elements based on the specified index and returns the deleted elements.

C: Modify function

Objectset (int index, Object obj): Modifies the element at the specified index position to the specified value, returning the value before the modification.

D: Get Features

Intindexof (Object O): Returns the index of the first occurrence of the specified element in the collection

Objectget (int index): Gets the element at the specified position

Listiteratorlistiterator (): List iterator

E: Interception function

listsublist (int fromIndex, int toindex): intercepts the collection.

Set Interface

The elements under the set interface are unordered and cannot be duplicated. The following are divided into HashSet and TreeSet.

HashSet

The underlying data structure is a hash table, thread insecure, and high efficiency.

Guaranteed Uniqueness depends on two methods: Hashcode () and Equals ().

Order:

Determines whether the hashcode () value is the same.

Same: Go ahead equals (), look at the return value

If true: It is not added to the collection.

If False: It is added to the collection.

Different: is added to the collection.

TreeSet

The underlying data structure is a two-fork tree, which is unsafe and highly efficient.

The method that guarantees element uniqueness is based on whether the return value is 0.

Two ways to guarantee sorting :

Natural ordering (elements are comparative): Implementing comparable interfaces

Comparator sorting (collection with comparison): Implementing Comparator interface


To be continued, the next article is the iterator +map collection


Java Foundation--Set (i)--collection system, collection collection

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.