Go Java Collection Framework Usage Summary __java

Source: Internet
Author: User

This article is an overview of the Java Collection framework, with the goal of having a general view of the Java Collection Framework system, and if you want to learn more about the specific interfaces and how classes are used, see the Java API documentation.

I. Overview

The data structure has a far-reaching influence on the program design, in the process-oriented C language, the database structures are described by struct, while in object-oriented programming, the data structure is described by the class, and contains the methods to operate the data structure.

In the Java language, the designer of the Java language makes some specifications (interfaces) and implementations of commonly used data structures and algorithms (the classes that implement interfaces specifically). All abstracted data structures and operations (algorithms) are collectively referred to as the Java Collection Framework (Java Collection Framework).

Java programmers do not have to consider the data structure and algorithmic implementation details in their specific applications, just use these classes to create some objects, and then apply directly to it. This greatly improves the programming efficiency.

Second, the hierarchy of the set frame

Collection is a collection interface

| ———— set Sub-interface: unordered, no duplication allowed.

| ———— list sub-interface: Ordered, can have duplicate elements.

Difference: Collections is a collection class

Set and list contrast:

Set: Retrieving elements is inefficient, deletion and insertion efficiency is high, insertions and deletions do not cause element position changes.

List: Similar to arrays, list can grow dynamically, find elements efficiently, and insert deletion elements inefficiently because it can cause other elements to change position.

Set and list-specific subclasses:

Set

| ———— hashset: Storing elements in the form of a hash table, inserting the deletion rate quickly.

List

| ———— ArrayList: Dynamic array

| ———— linkedlist: Linked lists, queues, stacks.

Array and Java.util.Vector

Vector is an old dynamic array that is thread-synchronized and inefficient and generally not in favor of use.

Iii. iterator iterator (interface)

Iterator is the process of getting the elements in the collection and actually helping to get the elements in the collection.

The iterator replaces the enumeration in the Java collections Framework. An iterator differs from an enumeration by two points:

Iterators allow callers to take advantage of well-defined semantics to remove elements from the collection that the iterator points to during the iteration.

The method name has been improved.

Iterator has only one sub-interface listiterator, a list iterator that allows programmers to traverse lists in either direction, modify the list during iteration, and get the current position of the iterator in the list. Listiterator has no current element; its cursor position is always between the element returned by the call previous () and the element returned by the call next (). In the list of length n, there are n+1 valid index values, from 0 to N (included).

Map interface outside the set frame

Map maps the key to the object of the value. A map cannot contain duplicate keys; Each key can map at most one value.

The map interface is a substitute for dictionary (dictionary) abstract classes.

The map interface provides three collection views that allow you to view the contents of a map in the form of a keyset, a value set, or a key-value mapping relationship set. The order of mappings is defined as the order in which the iterator returns its elements in the mapped collection view. Some mapping implementations can explicitly guarantee their order, such as the TreeMap class, and some mapping implementations do not guarantee the order, such as the HashMap class.

There are two common implemented subclasses:

HASHMAP: The implementation of MAP interface based on hash table. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. (in addition to being unsynchronized and allowing null, the HashMap class is roughly the same as Hashtable.) This class does not guarantee the order of mappings, especially if it does not guarantee that the order is immutable.

TREEMAP: It implements the SortedMap interface based on the red-black tree. This class ensures that the mappings are sorted in ascending order, depending on the constructed method used, which may be ordered in the natural order of the Key's classes (see comparable), or by the comparer provided at creation time.

Hashtable: This class implements a hash table that maps the key to the corresponding value. Any non-null object can be used as a key or value.

V. Thread-Safe classes

In the collection framework, some classes are thread-safe, and these are all present in the JDK1.1. After JDK1.2, there are a number of non thread-safe classes.

The following are the thread-safe synchronization classes:

Vector: There is more synchronization mechanism (thread safety) than ArrayList.

STATCK: Stack class, advanced later out.

Hashtable: More thread-safe than hashmap.

Enumeration: enumeration, equivalent to iterators.

In addition to these, the rest are non thread-safe classes and interfaces.

Thread-Safe classes its methods are synchronized and can only be accessed one at a time. is a heavyweight object with low efficiency. For non thread-safe classes and interfaces, programmers are required to handle thread-safety issues themselves in multiple threads.

Vi. Introduction to some other interfaces and classes

Dictionary and Hashtable classes:

Dictionary provides the function of a key-value mapping and is an abstract class. Its subclass Hashtable class is generally used. Iterate over the Hashtable class to use an enumeration.

Properties Class

Properties inherit from the Hashtable,properties class to represent a persistent set of properties. Properties can be saved in the stream or loaded from the stream. Each key and its corresponding value in the attribute list is a string. You can generally populate a Properties object by reading the properties configuration file.

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.