J2SE Summary (i)-------container

Source: Internet
Author: User

Recently, everyone is talking about containers and how to actually apply it in the project, because there is no concept of the container, so the j2se inside the container to see some basic knowledge, summed up the most basic things.

The core of the whole chapter belongs to the following picture.

I. Conceptual understanding

Why should have a container this concept: just contact, in my understanding: in programming, often encounter with duplicate functions or attributes of the object, often a large number of repetitive work leads to code redundancy, and as the user's needs constantly change, the number of changes is not constantly increasing, how to avoid a lot of changes, While reducing the coupling between modules (such as an AOP pool), we define the container, which is what the container is all about, all the objects inside the container have all the properties and methods of the container.

Containers: You can manage the life cycle of objects, dependencies between objects and objects, and you can use a configuration file (usually XML) that defines the name of the object, how it is produced (Prototype or singleton), Which object must be set to be a property of an object after the start of the container, all objects can be directly used, without writing any line of program code to produce objects, or to establish a dependency between objects and objects.

By the know, in the Java container, there are two branches, collection and map, where collection is a sequence of independent elements, list and set are two different forms of the implementation of the collection interface container, the list run objects are repeated and orderly, The objects of the set container are unordered and cannot be duplicated.

Map is a container of types of mappings between objects that behave as key-value pairs.

In encoding, if you define a container with properties and methods in it, then add an object to the container that also has the properties and methods of the container.

For example: The method defined in the collection interface, if a normal string or other object is added to the collection container, the L object will automatically have all the methods of collection.

Second, list and set

Implement it in the list interface with LinkedList and ArrayList

1/arraylist from the array, only the factor group must be fixed length, and the length is immutable, so with ArrayList to supplement its shortcomings, ArrayList has a list of features, can be multiplied according to the size of the object to expand memory space. The internal LinkedList is implemented in the form of a chain list.

2. Set

One of the only implementations of the set interface is HashSet, beginning to think of it as a hash table, and then knowing the mistake, which resembles a set of mathematics, unordered and not repetitive. That is, the same elements are not added to the collection.

public static void Main (string args[]) {          Set s=new HashSet ();          S.add ("Hello");           S.add ("Hello");           S.add ("World");          System.out.print (s);} Output [Hello,world], duplicate records will only be added once


Summary: The container, like its name, is used to host large amounts of data. At the same time can reduce a lot of duplicated code, with a certain technical support, you can reduce the coupling between modules and modules. Using containers can bring a lot of unexpected results to developers. or more in-depth discussion and practice.

J2SE Summary (i)-------container

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.