The purpose of the Java Container class library is to "save the object" and divide it into two different concepts:
- 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.
- 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 he associates numbers with objects. The mapping table 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 "dictionaries," because you can use a key object to look up a value object, as defined by using a word in a dictionary. 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. So, you can create a list like this:
list<apple> apples = new arraylist<apple> ();
Attention
ArrayListhas been transformed upward into
List。 The purpose of using the interface is that if you decide to modify your implementation, all you need to do is modify it at the creation point, just like this:
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,
LinkedListWith the
ListAdditional methods not included in the interface, and
TreeMapalso has in
MapMethod not included in the interface. If you need to use these methods, you cannot turn them up to a more generic interface.
CollectionThe interface outlines the concept of a sequence-a way to hold a set of objects. The following simple example uses the
IntegerObject is populated with a
Collection(Used here
ArrayList), and then print all the elements in the resulting container:
Package Com.mufeng.theeleventhchapter;import Java.util.arraylist;import java.util.collection;class Apple {}public Class Simplecollection {public static void main (string[] args) {collection<integer> C = new arraylist<> (); (int i = 0; i < ten; i++) {C.add (i);} for (Integer i:c) {System.out.print (i + ",");}}}
Because this example uses only the
Collectionmethod, so any inheritance from the
CollectionObjects of the class can work correctly, but
ArrayListis the most basic sequence type.
Add ()The name of the method indicates that it is to place a new element into the
CollectionIn However, the document is very carefully described in the "to ensure that this
CollectionContains the specified element. "That's because considering the
SetMeaning, because in
Setis added only if the element does not exist in the When using
ArrayList, or any kind of
ListWhen
Add ()Always means "put it in," because
ListDo not care if there is duplication.
All the
CollectionAll can be used
foreachSyntax traversal, as shown here.
Java Container Class Library Basic concept of a detailed