Four naming conventions-java Collection framework Overview

Source: Internet
Author: User
Tags define abstract union of sets to domain

Java is the most important and most commonly used part of a set. The ability to make good use of the collection and understanding of the collection has enormous benefits for Java program development. This article explains in detail how Java sets are implemented and their implementation principles.
A collection framework
1. Overview of Collection framework
1.1.1 introduction to containers
So far, we have learned how to create multiple different objects. After defining these objects, we can use them to do meaningful things.
For example, to store many employees, the difference between different employees is only the employee ID card number. We can store each employee in sequence through ID card numbers, but what is implemented in the memory? Is it necessary to prepare enough memory to store 1000 employees and then insert these employees one by one? If 500 records have been inserted, a new employee with a lower ID card number needs to be inserted. What should I do? Is a new record inserted from the beginning after all 500 records are moved down in the memory? Or create a ing to remember the location of each object? When deciding how to store the object set, you must consider the following issues.
The following three operations must be performed on an object set:
U add new object
U Delete objects
U lookup object
We must determine how to add new objects to the collection. You can add an object to a logical position at the end, start, or center of the set.
After an object is deleted from a collection, what is the impact of existing objects in the object collection? The memory may have to be removed, or a "hole" may be placed in the memory location where the existing object resides ".
After creating an object set in the memory, you must determine how to locate a specific object. You can create a mechanism to locate the target object based on certain search conditions (such as ID card numbers). Otherwise, you need to traverse each object in the set, until the object to be searched is found.
We have learned arrays before. An array is used to access a group of data. However, it has some disadvantages, so that it cannot be used to meet the requirements of the above application scenarios conveniently and quickly.
1. first of all, in many cases, we need to be able to store a group of data containers. Although Arrays can be implemented, it is not certain if we need to store the number of data. For example, we need to store all the current online user information of an application system in the container, and the current online user information may change at any time. That is to say, we need a data storage container, which can automatically change the size of the data that the container can store. In this regard, it seems very clumsy to use Arrays for storage.
2. Let's assume that a shopping website has stored a series of shopping lists after a period of running. The shopping list contains the product information. If we want to know how many types of products have been sold out during this time period. Then we need a container to automatically filter out duplicate information about the product in the shopping list. If arrays are used, this is also difficult to implement.
3. Finally, let's think about it. We often encounter this situation. I know the account name of a person and hope to learn more about other information about this person. That is to say, we store some user information in one place, and we hope to find some other information of the corresponding user through the user account. Let's take another dictionary look-up example: Suppose we want to use a container to store the word and explain the word. When we want to find the meaning of a word, you can find the corresponding word explanation in the container Based on the provided words. If array is used for implementation, it is more difficult.
To solve these problems, a container set is designed in Java. Different container sets store objects in different formats.
Mathematical background
In common usage, collections and mathematical sets have the same concept. A set is a unique item group, that is, there are no repeated items in the group. In fact, the "Collection framework" contains a Set interface and many specific Set classes. But the Formal Concept of set was a century ahead of Java technology. At that time, British mathematician George Boole formally defined the concept of set by logic. In elementary school, most people have learned some set theories through the "intersection of sets" and "union of sets" introduced by the familiar Wayne diagram.
The basic attributes of the set are as follows:
The u set contains only one instance of each item.
The u set can be limited or unlimited.
U can define abstract concepts
It is not only the foundation of logic, mathematics, and computer science, but also practical for the daily application of business and systems. The concept of "connection pool" is an open connection set of the database server. The Web server must manage the client and connection set. The file descriptor provides an example of another set in the operating system.
Ing is a special set. It is a pair set. Each pair represents one-way ing from one element to another. Some ing examples include:
U IP address to domain name (DNS) ing
U keyword ing to database records
U Dictionary (ing words to meanings)
Ing between u2's hexadecimal conversion and 10's hexadecimal conversion
Just like a set, the idea behind ing is much earlier than the Java programming language, or even earlier than the computer science. In Java, Map is a form of ing.
1.1.2 container category
Now that you have some set theories, you should be able to understand the "set framework" more easily ". A collection framework is composed of a group of interfaces used to operate objects. Different Interfaces describe different types of groups. To a large extent, once you understand the interface, you understand the framework. Although you always need to create interface-specific implementations, the methods used to access the actual set should be restricted to the use of interface methods. Therefore, you can change the basic data structure without changing other code. Shows the hierarchical structure of the Framework interface.
 
Java container class libraries are used to save objects and divide them into two different concepts:
1) Collection. A set of opposing elements, which generally follow certain rules. The List must maintain the specific sequence of elements, and the Set cannot have repeated elements.
2) Map. A pair of key-value pairs. It seems that this should be a Collection, and its elements are paired objects. However, this design is too clumsy, So we extract the Map explicitly to form an independent concept. On the other hand, if you use Collection to represent part of the Map content, it is easy to view this part of content. Therefore, Map can be easily extended into multi-dimensional Map without adding a new concept, as long as each "value" of the key-Value Pair in Map is also a Map.
The difference between Collection and Map is that the number of elements saved at each location in the container. Each position of the Collection can only save one element (object ). Such containers include: List, which stores a group of elements in a specific order; Set means that elements cannot be repeated.
Map stores "key-value pairs", just like a small database. We can find the "value" corresponding to the key through the "key ".
The ucollection-object does not have a specified sequence. Repeated elements are allowed.
There is no specified sequence between u Set-objects and repeated elements are not allowed
The ulist-objects are in a specified order. Repeated elements are allowed and the position subscript is introduced.
The u Map-interface is used to save the set of Key and Value. Each object in the Set provides values and keywords when added. The Map interface neither inherits Set nor Collection.
The basic implementation of List, Set, and Map is the Object array.
In addition to four historical collection classes, the Java 2 framework also introduces six collection implementations, as shown in the following table.
Interface
Implementation
Historical collection class
Set
HashSet
 
 
TreeSet
 
List
ArrayList
Vector
 
Shortlist
Stack
Map
HashMap
Hashtable
 
TreeMap
Properties
 
There is no Collection interface implementation here. Next we will look at the big image of the Collection framework in detail:
This figure looks a little scary. Once you are familiar with it, you will find that there are only three containers: Map, List, and Set. Each of them has two or three implementation versions. Commonly used containers are displayed in a black rough box.
The dot-line box represents the "interface", the dotted box represents the abstract class, and the solid-line box represents the common class (that is, the specific class, rather than the abstract class ). The dotted arrow indicates that a specific class implements an interface (in the case of an abstract class, it is "partially" that implements that interface ). The solid arrow indicates the object of the class to which the arrow can be generated. For example, any Collection can generate an Iterator. In addition to a ListIterator, a List can generate a common Iterator, because List is inherited from the set.
Author: pplsunny

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.