Java Common knowledge (Mian shi) point Summary---Collection

Source: Internet
Author: User

First, the difference between Collection and collections:
1. Collections:
Java.util.Collections is a wrapper class. It contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated, just like a tool class that serves the Java collection framework.


2. Collection:
Collection is the most basic set interface, which defines a set of objects that are allowed to be duplicated, provides a common interface method for basic manipulation of the collection objects, and the meaning is to provide the maximum uniform operation for various specific collections.


Second, Collection Foundation:
1. The collection interface derives two sub-interface sets and lists, respectively defining two different storage modes, as follows:

(1) List is a collection class that implements the list interface, ordered and repeatable, two important implementations of ArrayList and LinkedList, with the following characteristics:
ArrayList: The bottom layer is implemented by maintaining an object array. Features: Fast query speed, adding and deleting slow.
LinkedList: The bottom is the use of two-way linked list data structure implementation, features: Slow query speed, and delete quickly.
Vector (not commonly used): The bottom layer is also maintained an object array implementation, the implementation and ArrayList is the same, but the vector is thread-safe, operating efficiency is low.

(2) Set is a set class that implements the set interface, unordered and non-repeatable. Two important implementations of class HashSet and TreeSet
HashSet: The bottom layer is supported using a hash table, which features: fast access speed.
TreeSet: If the elements have natural order characteristics, they are sorted by the nature of the elements ' natural order.


2. Methods in the collection interface:

(1) Increase
Add (e E) Adds a successful return of true, adding the failed return false.
AddAll (Collection c) adds the elements of a collection to another collection.

(2) Delete
Clear ()
Remove (Object o)
RemoveAll (Collection c)
Retainall (Collection c)

(3) View
Size ()

(4) Judgment
IsEmpty ()
Contains (Object o)
Containsall (collection<?> c)

(5) Iteration
ToArray ()
Iterator ()

3. Map&iterator interface and comparable interface
1) Map Interface:
This interface describes a key-to-value mapping that is never duplicated. The map interface is used to maintain key/value pairs (Key/value pairs).
Feature: It describes a mapping of key-to-value values that are never duplicated. Two important implementation classes: HashMap and TreeMap
HASHMAP: Based on hash table, the characteristic is the mapping relation of key-value pairs. A key corresponds to a value. The ordering of elements in HashMap is not fixed. More suitable for inserting, deleting, and positioning elements.
TREEMAP: Based on red-black tree. The elements in the TreeMap maintain some sort of fixed order. More suitable for sequential traversal of elements.

2) Iterator Interface:
A collection accessor that iterates through the objects in the collection. All container classes that implement the collection interface have a iterator method that returns an object that implements the iterator interface.
The iterator object is called an iterator, and the iterator interface method iterates through each element of the collection iteratively, and can remove the appropriate element from the collection.

3) Comparable interface:
Implementations that can be used for comparisons, classes that implement the comparable interface can determine how the class objects are sorted by implementing the Comparato method.

Iii. the difference between HashMap and Hashtable
1. HashTable:
Thread-safe, multiple threads can share a hashtable, and multiple threads cannot share hashmap without proper synchronization.
Java 5 provides Concurrenthashmap, an alternative to Hashtable, which is more scalable than Hashtable, and is slower than HashMap in a single-threaded environment.

2. HashMap:
Non-synchronized, and can accept null (HashMap can accept NULL as a key value (key) and value, while Hashtable is not.
HashMap cannot guarantee that the order of elements in the map will be constant over time.
In addition, the HashMap iterator (Iterator) is a fail-fast iterator, and Hashtable's enumerator iterator is not fail-fast.

To be Continued ...

Java Common knowledge (Mian shi) point Summary---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.