Collections in Java-oriented objects

Source: Internet
Author: User

1. What are the reasons for learning a collection?

A. Arrays are process-oriented, and collections are object-oriented.

B. Collections are classes, with classes that encapsulate, inherit, polymorphic ... Ultra-strong function.

C. Array is fixed length, set is variable length

D. The array has no way to get the true number of elements;

E. Arrays have only one fixed method of stored value (sequential structure stored value), and the collection has many ways, which are suitable for many occasions.


2. Family members of the collection:

(First block:)
Collection "unordered, not unique" Map "key, value"
"Unordered, unique" "unordered, not unique"

List "ordered, not unique" set "unordered, unique"


ArrayList linkedlist Vector HashSet TreeSet HashMap
|         |           |     | |
"Sequential storage" "Linked list Storage" "Hash table" "Binary Tree" "Hashtable + key value pair"


3. Introduction ArrayList:

Stored value mode: Sequential structure storage.

Use cases: traversal and random access.

Common methods:

Added: Add (); AddAll (Collection);

Delete: Remove (object or subscript); RemoveAll (Collection);

Find: Contains (Object); Containsall (Collection); size (); get (subscript);

Iteration: Iterater:

Use:
Iterator iter =list.iterator ();
while (Iter.hasnext ()) {
System.out.println (Iter.next ());
}

4. Introduce the difference between vector and ArrayList: (Learn)

1. Same point: same principle, same underlying code, same method

2. Different: Vector thread-safe, 1 time times increase each time, old version JDK provides an interface for Operation Collection

ArrayList high efficiency, thread insecure, 0.5 times times increase each time, ArrayList is a new version of the interface that provides the collection.

5. Introduction LinkedList:


Stored values: Linked list structure

Use occasions: Delete and modify

Method:

Added: Add (); AddAll (); AddFirst (); AddLast ();

Delete: Remove (); clear (); RemoveAll ();
Removefirst (); Removelast ()

Search: Contains ("Beijing"); Containsall (list1); size (); get ();
GetFirst (); GetLast ();

Iteration: Iterater:

Compare the differences between LinkedList and ArrayList:

1. The method of storage value is different: sequential structure, linked list structure

2. Different use situations: traverse and random access; Delete and modify


6. Introduction to Generics:

What are the reasons for learning generics?

Ensure that the collection has no problem with the "compile-time" value, then the run-time value is fine.

function: In "Compile period", control the type of receiving element.

Generics are used in collections:

linkedlist<integer> link = new linkedlist<integer> ();

Link.addfirst (567);
Link.add (12);
Link.add (111);
System.out.println (link);

Iterator<integer> List=link.iterator ();
while (List.hasnext ()) {
Integer I=list.next ();
System.out.println (i+1);
}

7. The second block of the collection:

Map (Key-value pair, key unordered unique; value unordered not unique)

HashMap TreeMAP HashTAble


(7.1) Introduction: HASHMAP

Stored value method: Hash table stores key value pairs.

HashMap vs Hashtable (Learn)

1. Hashtable old JDK version; HashMap new version

2. Hashtable thread safety, HashMap high efficiency, thread insecure.

3. HASHMAP implements the map interface; Hashtable inherits Dictionary.

4.HASHMAP allows null values, Hashtable does not allow null values

Same: Principle, algorithm, the same as the stored value method.

Method:

Added: Put (011, "Beijing"); Putall (MAP1);

Remove: Remove (key); ---to delete value according to key

Query: Get (Key)--to delete value according to key.
KeySet ()---The collection of return keys
VALUES ()---a collection of return values
ContainsKey (Key)---returns True if there is a "key-value pair" that is mapped by the specified key

Collections in Java-oriented objects

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.