Crazy Java Learning notes----------Iterator, collection interface, and foreach

Source: Internet
Author: User

Iterator, Collection interface:

such as: Iterator, collection in a package:

The Scarlet letter part makes us often encounter, but encounters does not know how to understand, to apply it!

Collection is the most basic set interface, which defines a set of objects that are allowed to be duplicated. The collection interface derives two sub-interface sets and lists, respectively defining two different storage methods, as follows:

2. Set interface

The set interface inherits from the collection interface, which does not provide an additional method, but the elements in the collection class that implement the set interface are unordered and non-repeatable.

Features: unordered and non-repeatable.

3. List interface

The list interface also inherits from the collection interface, but, contrary to the set interface, the elements in the collection class of the list interface are object-ordered and repeatable.

Features: ordered and repeatable .

Two important implementation classes: ArrayList and LinkedList

1.ArrayList features are ordered and repeatable

2.LinkedList is a doubly linked list structure .

4. Map Interface

Map is also an interface, but does not inherit the collection interface. This interface describes a key-to-value mapping that is never duplicated. The map interface is used to maintain key/value pairs (Key/value pairs).

5.Iterator interface

iterator , there is an exception in C # IEnumerator, they are all collection accessors that iterate over the objects in the collection.
All container classes that implement the collection interface have a iterator method that returns an object that implements the iterator interface. The iterator object is called an iterator,and theiterator interface method iterates through each element of the collection iteratively , and can remove the appropriate element from the collection.

The following will be the real strength of the show!

Collectiontest.java Code


Package Com.haixu.collection;import Java.util.arraylist;import Java.util.collection;import java.util.HashSet;/** * Collection collection of internal methods of learning! * */public class Collectiontest {public static void main (string[] args) {collection<string> C =new arraylist&lt ; String> (); add Element C.add ("hi!") to the collection; C.add ("Haixu"); Output individual elements System.out.println ("number of collection elements of C:" +c.size ());  Although the base type value cannot be placed in the collection, Java supports automatic boxing! Deletes the specified element C.remove (2);  System.out.println ("The number of collection elements of C:" +c.size ());  Determines whether the set C contains the string "Haixu" System.out.println ("C" in the collection with \ "Haixu\" string: "+ c.contains (" Haixu ")); Continue adding elements to the collection and outputting the collection elements C.add ("You are very perfect!");  System.out.println ("The elements in the collection of C are:" + C);    Define objects with Hsahset c1 collection<string> c1 = new hashset<string> (); add Element C1.add ("Believe yourself!"); C1.add ("haixu!"); C1.add ("You'll success!");  SYSTEM.OUT.PRINTLN (C1); Determines whether the C collection fully contains the C1 collection System.out.println ("Does the C collection fully contain the C1 collection?").  "+ c.contains (c1)); C.removeall (C1);  System.out.println ("The set element of C is:" + C); Empty set C c.clear (); System.out.println ("C's Set element is:" + C); C1.retainall (c); SYSTEM.OUT.PRINTLN ("C1 set elements are:" + C1); }}


Iteratortest.java

Package Com.haixu.collection;import Java.util.collection;import Java.util.hashset;import java.util.Iterator;/* *  Iterator used to traverse the collection * */public class Iteratortest {public static void main (string[] args) {    collection<string> str1 = new hashset<string> ();  add element  Str1.add ("Everything that can do!");  Str1.add ("If only do work continuely!");  Str1.add ("Success'll belong to you!");    Gets the iterator that corresponds to the STR1 collection  iterator<string> it = Str1.iterator ();  while (It.hasnext ()) {      string str = (string) it.next ();   System.out.println (str);      if (Str.equals ("Everything that can do!"));   It.remove ();      str = "Everything that can do!";      System.out.println (str);  }  System.out.println (STR1);   } }



The Foreach loop iterates over the elements in the collection collection and the home is a little more concise!

The Foreach loop iteration Azimuth collection element is that the set cannot be changed or the concurrentmodificationexception exception will be thrown!

Foreeachtest.java

Package Com.haixu.collection;import Java.util.collection;import java.util.hashset;/* * Foreeach Loop to iterate over elements in the Collection collection * */public class Foreeachtest {public static void main (string[] args) {collection<string> str 1 = new hashset<string> ();//add Element Str1.add ("Everything that can do!"); Str1.add ("If only do work continuely!"); Str1.add ("Success'll belong to you!"); for (Object obj:str1) {string str = (string) obj; System.out.println (str); if (Str.equals ("Everything that can do!")) {//The following code throws an exception//IS intentional! Str1.remove (str);}} System.out.println (STR1);}}


The above is the application and usage of each interface.

Because there is a iterator () method that returns a value of type iterator<e>, the collection interface must implement the iterator interface

Each of the classes that implement the collection interface implements many of these methods, but the developer itself is cumbersome to implement. So Java provides the Abstractcollection class to write a specific class.

The following classes implement the collection interface:

abstractcollection, abstractlist, abstractqueue,  Abstractsequentiallist, abstractset, arrayblockingqueue, ArrayDeque ,  arraylist ,  attributelist, beancontextservicessupport,  BeanContextSupport, ConcurrentLinkedDeque,ConcurrentLinkedQueue, ConcurrentSkipListSet,  Copyonwritearraylist, copyonwritearrayset, delayqueue, EnumSet ,  hashset , jobstatereasons, linkedblockingdeque,  Linkedblockingqueue, linkedhashset ,   LinkedList , Linkedtransferqueue, priorityblockingqueue,  Priorityqueue ,  rolelist, roleunresolvedlist, stack, synchronousqueue, treeset ,  vector

Crazy Java Learning notes----------Iterator, collection interface, and foreach

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.