Java-preliminary Understanding-Common Object API (collection framework-system commonality function)

Source: Internet
Author: User
Tags addall

One.

A collection is a container that is used to store objects. When the number of objects we store becomes more, there are some problems.

When the object is loaded in the collection, how exactly is it stored? Some people say that it is just to throw it in, note that for objects, there is more than one container set.

Each collection has its own unique definition of how objects are stored, based on certain requirements.

For example, to talk: Tea with water cups, must have a filter, or tea will have tea, if just drink plain boiled, then there is no need to filter the net.

This means that the container itself has its own specific structure, which we call the data structure, which is stored in the container.

What is the way data is stored in the end? We call this the structure of the data. The structure of each container is not quite the same inside.

A bunch of containers, tall and thin are not the same, can go to install things, but they have their own internal characteristics. Depending on the needs, different containers are used.

In many different containers, some of the containers have commonalities. In this way, continuous extraction, partial extraction, continuous upward, forming a system. (Cat, dog ...) An animal, too soon.

Set frame system, there are a lot of content. Such a huge system is not scary, the way to learn is: Look at the top layer, with the bottom.

The top layer defines all the common things in the collection system and wants to use the set system, so long as the function of the top layer is clear, any member of the system can use it.

Commonality is present in the set, called Collection, which is the top layer that is continuously drawn upward.

What kind of method does this top layer have? If you want to determine what kind of method a container has, according to the object-oriented way of thinking, what is the function of this container to facilitate my command? Or what kind of function this object has to facilitate my command. The most basic function of a container is to install something and delete something.

Now take a look at what collection is capable of.

Two.

Now we are learning the collection system, which is not in the Java.lang package. Java.lang package is the Java language Core package, now we are using a tool, which is the contents of the toolkit, called the Java.util package.

Collection since it appears as an interface, then it means that the contents are all abstract. Next, learn about the common interface of collection, which defines which methods. When collection is finished, the use of the collection is not a problem.

1. Add a method

The Add method is the addition of objects, what does e mean? Now use E As object (can accept arbitrary object, it must be object type, so here says E is object). The 1.4 version says that object,1.5 has a new feature, so there is a change. Now, without introducing new features, learn the use of collections before learning the use of collections.

Add a method, give me an element to add to the collection, the return value type is Boolean. Add success is true, add failure is false.

The AddAll method is to add all the elements from one collection to another, not one, possibly a heap.

In fact, the wording is AddAll (Collection coll);

2. Delete method

There are two ways to delete, either delete or remove.

Both added and deleted return value types are Boolean, which indicates that these actions are not simple operations and may involve many things.

RemoveAll is to delete a bunch and should not all be deleted. If all is deleted, it is not the Remove method, it is the clear method, is the meaning of empty.

Clear deletes all the elements of the collection, but the collection is still there.

3. Judging

A Boolean Contans method that determines whether an element, or a bunch of elements, is in the collection. ( I will confuse judgment and search )

The Boolean Containsall (collection coll) determines whether a collection has a bunch of elements and needs to make a collection of the elements that are judged first ? Otherwise, how is the parameter list passed to a collection?

The judgment of the set can also be used hashcode,equals.

The IsEmpty method is to determine if there are elements, and this method will conflict with other methods.

4. Get

There are several ways to get methods, one is to get the number (or length) of the elements in the collection, and there are several elements in the collection that are most clearly set. Can get the length of the method, no outside length and size. If size is 0, there is no element. The IsEmpty method is true,size if there are elements, the IsEmpty is false. The collection is judged by size, but encapsulated as a isempty.

The iterator method that gets the binding element is the iterator. An iterator is a method that is specifically used to remove elements from the collection.

The return value of the iterator is very special, not the element being fetched, but an iterator object interator.

5. Other methods

(describe many of the methods in a class in a categorized way)

The Ratainall method is the same as RemoveAll, which is listed separately because this method is the intersection of two sets.

ToArray is to change the collection into an array,

Java-Preliminary Understanding-Common Object API (collection framework-system commonality function)

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.