Thinkinginjava Study Notes 10_ container

Source: Internet
Author: User
Tags addall map class

Java does not have direct support for containers like Perl, Python, and Ruby, but can rely on container classes to do the same thing;

Generic type

A ArrayList object can be used to save a series of objects, such as: ArrayList apples = new ArrayList (); Declares a ArrayList object apples, which can then be added to the previous Apples object: Apples.add (object), or the object can be obtained from the index: Apples.get (index), at which point the objects are placed in object type, This is an unsafe type, because there is no guarantee that all objects will use the same interface, and when two different types of objects are placed inside, some problems arise.

In order to use a more secure container, generics are required, as shown in the sample code, using a pair of angle brackets to represent the generic:arraylist<apple> apples = new arraylist<apple> (); If you add other types of objects to the container, the compiler will find and error it, and you can see that generics support the upward transformation;

Type of container

Containers can be divided into two types:

1. Collection, the sequence object of the individual elements, including: List, Set, Queue;

2. Map, a pair of "key-value pairs" object;

Container type use, create a concrete class of objects, and then transform to the corresponding reference, and then programming for the interface, such as: ArrayList is a subclass of the list, you can declare:

list<apple> apples = new arraylist<apple> ();

At this point, ArrayList is transformed upward into a list, to ensure that only the interface in the list and not use the unique interface of ArrayList, such a declaration can make the object's specific class changes more convenient, such as change to LinkedList:

list<apple> apples = new linkedlist<apple> ();

Without changing any of the other code, because all the work is done on the interface of the list;

All collection support the foreach syntax;

The Collection.addall () method allows you to add a collection object to complete the initialization, and a new empty collection object, and then the Collections.addall () method to add the object is faster, Is the preferred initialization method, as in the example code, the new Collections.addall adds more elements to the first parameter: The collection object, and the same arrays.aslist () can generate ArrayList objects more quickly;

Arrays.aslist () may produce some problems, and can not be based on the left side of the upward transformation, need to display the type of declaration, such as: Arrays.<snow>aslist (), for accurate operation;

List

The list class consists of two types:

ArrayList: Random access to elements in an array is faster, but inserting elements and deleting elements is slow;

LinkedList: Accessing elements is slow, but inserting and deleting elements is faster;

Specific ways to view the manual: http://docs.oracle.com/javase/7/docs/api/java/util/List.html

LinkedList provides support for queue and stack, and because of design flaws, it is best to eliminate the use of stack and use LinkedList instead;

Map

The Map class contains:

HASHMAP: Designed for quick access, where keys are stored in disorder;

TreeMap: The key is always in the sort state, the access speed is slow;

Linkedhashmap: Preserves the insertion order of elements while providing fast access;

Summarize

This chapter is about some of the broken, better is the following classification chart:

Where the dashed box represents the interface, the solid wireframe represents the concrete class, the Black Line box is a more commonly used container; a hollow arrow indicates a specific class solid line an interface, a solid arrow indicates that a class can generate objects that the arrows point to the class;

Just read the documentation, feel clearer and more specific.

Thinkinginjava Study Notes 10_ 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.