Thinking in java--collection (Collection)

Source: Internet
Author: User

First, the use of ArrayList (slightly)

Second, the basic concept of the container

(a), collection is the basic interface of the collection class

Main methods:

 Public Interface Collection<e>{  boolean Add (E element);   adds an element to the collection, E represents the generic  iterator<e> Iterator ();   Returns an object that implements the iterator interface }

About: Iterator after the lecture.

(b) The subclass of the collection is realized:

List: Inserts the saved element in order. Set: Cannot have duplicate elements in the container queue: determines the order of the objects according to the rules of the queue

(c), map interface

Definition: A pair of "key-value pairs" objects, using key to find value

Note: This is a separate interface and does not belong to the collection

(iv) Use of containers in accordance with the analysis of appeals

1, ArrayList upward transformation to collection, and through the collection add processing data. (Technique applied: Parent reference to Child class object, method of calling subclass)

 Public classuptocollection { Public Static voidmain (String [] args) {ArrayList<Integer> arrayList =NewArrayList ();  for(inti=0; i<5; ++i)        {Arraylist.add (i); } Collection<Integer> collection =arrayList; Collection.add (5); //The Foreach loop is used here, as long as the iterator interface is implemented to use the foreach//collection does not have a GET method for list, need to use iterator to traverse         for(Integer i:collection) {System.out.println (i); }    }}
View Code

2. Add a set of elements

 Public classaddelementtocollection { Public Static voidMain (String[]args) {Collection<Integer> collection =NewArraylist<integer> (Arrays.aslist (1,2,3,4,5,6)); //Knowledge point 1: Use arrays, a static class, to convert an array, or a column of numbers, to a list//Knowledge point 2:arraylist can be nested list creation        /*Knowledge point 3: Why can not collection<integer> collection = * Arrays.aslist (1,2,3,4,5,6), and found that the creation is not a problem, but to the following time use * Collection.addall (Arrays.aslist (datas)); * Cause: Because the list created by Arrays.aslist (1,2,3,4,5,6) is a fixed-length list * It is not         Executes the Add () method. */Integer [] datas= {7,8,9,10}; /*Knowledge point 4: You must use integer as the class, you cannot use int, otherwise you will get an error * because you have previously collection<integer> collection, you have set the generic to integer * so you cannot use reference types To get an array of what classes the input generics are going to need to build*/Collection.addall (Arrays.aslist (datas));  for(Integer i:collection) {System.out.println (i); }        //The previous step is to add a set of data (arrays, or custom data) to the list via collection, and another method is described belowCollections.addall (Collection,11,12,13,14,15,16);        Collections.addall (collection, datas); //Note: This is collections class is not collection class    }}
addelementtocollection

Collection The method of adding elements Advantages: The method runs efficiently. Cons: But you need to create a collection class to act as an object

Collections the method of adding elements advantages: Convenient, do not need to create collection class.

3, the role of collection type: Unified type Add, delete ... (Unified traversal is determined by iterator)

 Public classCollectioneffect {//unify a variety of containers, just one way to add, delete     Public Static voidMain (string[] args) {print (NewArraylist<string>())); Print (Fill (NewHashset<string>())); Print (Fill (NewHashmap<string,string>())); }    //a collection of collection interfaces     Public StaticCollection<string> Fill (collection<string>c) {C.add ("Dog"); C.add ("Cat"); C.add ("Pig"); returnC; }    //overloaded Fill method, providing a map interface     Public StaticMap<string,string> Fill (map<string,string>map) {Map.put ("Dog", "pig"); Map.put ("Cat", "small"); returnmap; }         Public Static voidPrint (collection<string>c) {            }         Public Static voidPrint (map<string,string>map) {            }}
CollectioneffectThree, iterators (Iterator)

function: Traverse all kinds of containers uniformly

Interface method:

 Public Interface Iterator<e>{  E next (); // gets the next element  boolean hasnext (); // See if there is a next element  void Remove (); // remove Element }
Iterator

Thinking in java--collection (Collection)

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.