Basic Concepts---java containers

Source: Internet
Author: User

1. Holding objects

The purpose of the Java Container Class library is to "save the object" and divide it into two different concepts:

(1) Collection: A sequence of independent elements that obey one or more rules. The List must save the element in the order in which it was inserted, and Set cannot have duplicate elements. The queue determines the order in which objects are produced, usually in the same order in which they are inserted, according to the queuing rules.

(2) Map: A pair of " key-value pairs " objects that allow you to use keys to look up values. ArrayList allows you to use numbers to find values, so in a sense it associates numbers with objects. Mapping allows us to use another object to find an object, also known as an associative array, because it associates some objects with other objects or is called a "dictionary," because you can use a key object to find a value object, just as you would use a word in a dictionary to define it.

2. Interface-oriented programming ideas

Map is a powerful programming tool. Although not always, in an ideal situation, most of the code you write is dealing with these interfaces , and the only place you need to specify the exact type you are using is when you create it. You can create a List like the following :

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

Therefore, you should create an object of a specific class, transform it into the corresponding interface, and then use that interface in the rest of the code . This approach does not always work because some classes have additional functionality, for example, LinkedList has additional methods that are not included in the list interface, and TreeMap also has methods that are not included in the map interface. If you need to use these methods, you cannot turn them up to a more generic interface.

3. Generic and type-safe containers

one of the main problems with using Java SE5 containers is that the compiler allows you to insert an incorrect type into the container. For example, consider a container for an Apple object, and we use the most basic and reliable container ArrayList. Now, you can think of ArrayList as "an array that can automatically expand its size." Using ArrayList is fairly simple: Create an instance, insert an object with Add () , and then access those objects with get () , where you need to use an index, just like an array, but no square brackets are required. ArrayList also has a size () method that allows you to know how many elements have been added, thus causing an error to be raised without the risk of being accidentally crossed.

If we put both Apple and Orange in the container, then take them out. Normally, the Java compiler reports warning messages because generics are not used. Apple and orange classes are different, except that they are not anything other than object ( if a class does not explicitly declare which class it inherits from, it automatically inherits from object). Because ArrayList holds an object, you can not only put the Apple object into the container through the ArrayList Add () method, you can add an orange object, and there is no problem at compile time or run time.

But when you use the get () method of ArrayList to take out an object that you think is Apple, you get a reference to object that must be transformed into Apple, so You need to enclose the entire table edge, forcing the transformation before calling the Apple's ID () method. Otherwise, you will get a syntax error. At run time, when you try to transform an orange object into an apple elbow, you get an error in the form of the exception mentioned earlier.

Using Java generics to create a class can be more complex. However, it is often easy to apply predefined generics. For example, to define a ArrayList to hold an Apple object, you can declare arraylist<apple>, not just ArrayList, where the type parameter is enclosed in angle brackets. (can have more than one), which specifies the types that this container instance can hold. by using generics, you can prevent objects of the wrong type from being placed in the container at compile time.

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

now, the compiler can prevent you from placing orange in apples, so it becomes a compile-time error and is no longer a runtime error. You should also notice that the type conversion is no longer necessary to remove an element from the list. Because list knows what type it holds, it will perform the transformation for you when it calls get (). Thus, by using generics, you not only know that the compiler will examine the types of objects you place in the container, but you can use clearer syntax when using objects in the container.

All Collection can be traversed with the foreach syntax, but the class called " iterator " is more flexible.

2018-01-01 content from the Java programming idea

Basic Concepts---java containers

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.