JAVA OOP Collection Framework

Source: Internet
Author: User

1.Collection is the ancestor interface of the collection class, and the main interface for inheriting it is set and list. Collections is a helper class for a collection class that provides a series of static methods for searching, sorting, threading, and so on for various collections.

The main differences between arrays and collections

2. (1) An array can store basic data types and objects, and only objects can be stored in the collection (the base data type may be stored as a wrapper class).

(2) The length of the array is fixed, and the set length can be changed dynamically.

(3) When defining an array, you must specify the array element type, and the collection defaults to all of its elements are object

(4) The number of elements actually stored in an array cannot be obtained directly, and length is used to get the lengths of the array, but the number of elements actually stored by the collection can be obtained directly through size ().

(5) The set has a variety of implementation and different applications, rather than the array is only used to allocate a continuous space mode

(6) A collection exists in the form of an interface and a class. With the characteristics of encapsulation, inheritance and polymorphism, a variety of complex operations can be realized by simple method and property invocation, which greatly improves the development efficiency of software.

3. The most common collection classes are List and Map. The specific implementation of the list includes ArrayList and vectors, which are variable-sized lists that are more appropriate for building, storing, and manipulating any type of object. List is useful for cases where elements are accessed by numeric indexes.

The seventh chapter sets the frame 1. If you do not know how many objects are required to run the program, or if you need more complex ways to store objects-you can use the Java Collection Framework 2.Java collection framework to provide a set of good performance, easy-to-use interfaces and classes that are located in the Java.util Package 3. (1) interface (2) Specific class (3) algorithm: collection--provides a set of sorting, traversal and other algorithm implementation note: The collection interface stores a set of not unique, unordered objects; The list interface stores a unique set of objects that are ordered (in order of insertion); The set interface stores a unique set of Unordered Object 4. (1) The map interface stores a set of key-value objects that provide a key-to-value mapping (2) ArrayList implements a variable-length value that allocates contiguous space in memory (a higher efficiency of traversing elements and random access elements) 5. Common methods of List
Method name Description
Boolean Add (Object o)

Add elements in the order of the end of the list.

Starting index position starting from 0

Voidadd (int index,object o)

Adds an element at the specified index location.

The index position must be between 0 and the number of elements in the list

Intsize ()

Returns the number of elements in a list

Objectget (Intindex)

Returns the element at the specified index position. The removed element is of type object and requires a forced type conversion before use

Boolean contains (Object o)

Determines whether the specified element exists in the list

Boolean remove (Object o)

Remove an element from the list

Object Remove (int index)

Removes the specified position element from the list,

Starting index position starting from 0

Note: LinkedList provides a special method for adding and removing operations to the head and tail elements 6.LinkedList
Method name Description

void AddFirst (Objecto)

Add an element to the header of the list

void AddLast (Objecto)

Add an element at the end of the list

Object GetFirst ()

Returns the first element in a list

Object GetLast ()

Returns the last element in a list

Object Removefirst ()

Delete and return the first element in a list

Object Removelast ()

Delete and return the last element in the list

Common methods of 7.Map /tr> If the specified key does not exist /tbody>
method name Description

Objectput (Object key, Object val)

Stored as key-value pairs

Objectget (Object key)

Returns the associated value according to the key, or null

Objectremove (Object key)

Delete key-value pairs that are mapped by the specified key

Intsize ()

Returns the number of elements

setkeyset  ()

Return a collection of keys

Collectionvalues ()

Return a collection of values

Boolean  containskey (Object key)

Returns True if there is a key-value pair that is mapped by the specified key

8. Collection class comparison (1) vector and ArrayList the similarities and differences to achieve the same principle, function the same, can be used to the main difference:? Vector thread safety, ArrayList heavy speed, light security, thread non-security? When the length needs to grow, the vector increases by one times by default, ArrayList growth 50% (2) Hashtable and hashmap the similarities and differences to achieve the same principle, function the same, can be used the main difference:? Hashtable inherit dictionary class, HashMap implement Map interface? Hashtable thread safety, HashMap thread is not secure? Hashtable does not allow null values, HASHMAP allows null values 9. Iterator Iterator Method 1: Iterate through the iterator implementation of the Iterator:collection () method to get the iterate interface Iterator method? Booleanhasnext (): Determine if there is another accessible element? Object Next (): Returns the next element to access Method 2: Enhanced for loop

for (element type T element variable x: Array or Collection Object) {

Java statements that reference X

}

Note: The two can also traverse other collection classes, usually with the addition of a for-loop

JAVA OOP Collection Framework

Related Article

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.