Java container knowledge summary - this one is enough

Container knowledge summary:

 

1. Collection interface _

Common methods in the Collection interface:

 

Size()

isEmpty()

clear()

contains()

add()

remove()

iterator() : returns its pointer

 

2. List ( ArrayList & LinkedList ) : Inherited from Collection , adding the following common methods:

 

get(int index)

set(int index,Object element) : set the value, different from add

indexof(Object o) : returns the index

 

3. Set ( HashSet & TreeSet ) : inherited from Collection , no additional methods are added;

 

4. Map ( HashMap & TreeMap ) : Commonly used methods are as follows:

 

put(Object key,Object value)

get(Object key)

remove(Object key)

boolean containsKey(Object key)

boolean containsValue(Object value)

int size()

Boolean isEmpty()

clear()

 

5. Collections class : provides some static methods to implement some common algorithms based on List container :

 

void sort(List) : sort

void reverse(List) : reverse

void shuffle(List) : random sorting

void fill(List,Object) : rewrite List with specific object

void copy(List desc,List src) : copy src to desc

int binarySearch(List, Object) : For a sequential List container, use the half-search method to find a specific object

 

6. Iterator interface : Each Collection interface and its sub-interfaces have an iterator () method that returns a pointer to its object. The pointer starts at the left of the first element. This interface has only three methods:

 

Boolean hasNext() : Whether there is an element to the right of the pointer

E next() : returns the element to the right of the pointer

Void remove() : delete the element to the left of the pointer, this operation can only be performed once after executing next

 

7. Comparable interface : To compare the sizes of containers , the Comparable interface is often inherited , and its comparaTo method is rewritten to define the comparison rules:

 

Java.lang.Comparable (note the interface under the java.lang package)

public int compareTo(Object obj)

Return 0 : means: this = obj

Returns a positive number: means: this > obj

Returns a negative number: means: this < obj

 

 

8. The difference between Collection and Collections :

Collection is the parent interface of list and set , and Collections only provides some common tool methods for List interface

 

In general, just remember a diagram:



 

Set : no order, not repeatable

List : in order, repeatable

Map : store key-value test tests

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646986&siteId=291194637