The collection interface in Java--list, set, Map

Source: Internet
Author: User
Tags object object

The collection interface in Java--list, set, Map

What is a collection: A collection is an instance of a series of classes provided by the Java API that can be used to store multiple objects dynamically. That's the same as the array we've learned, so why do we have to learn about collections and see what the difference is between arrays and collections:

Array: fixed length, fast traverse speed can store basic type and reference type;

Set: length is not fixed , only reference type object can be stored;

From the characteristics of arrays and sets we can clearly see that arrays have certain limitations, if we need to store data in our data irregularly, the array will not meet our requirements.

The Java Collection Framework provides a set of well-performing, easy-to-use interfaces and classes that are located in the Java.util package

The Collection interface stores a set of not unique, unordered objects; The List interface stores a set of objects that are not unique, ordered (in order of insertion);

The set interface stores a set of unique, unordered objects, and the map interface stores a set of key-value objects that provide a key-to value mapping

For some of the methods in the collection interface, let's use the actual code to learn:

Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator;/*** Collection * Set BASIC operation * Add * Delete * traverse * judgment * Find *@authorShen_hua **/ Public classCollectiondemo { Public Static voidMain (string[] args) {Collection<String> collection=NewArraylist<string>(); System.out.println ("Number of elements:" +collection.size ()); //adding elementsCollection.add ("AAA"); Collection.add ("BBB"); Collection.add ("CCC"); System.out.println ("Number of Added elements:" +collection.size ());//        //Delete Element//collection.remove ("BBB");//System.out.println ("Number of elements after deletion:" +collection.size ());//collection.clear ();//System.out.println ("Number of elements after emptying:" +collection.size ()); //enhanced for Loop traversalSYSTEM.OUT.PRINTLN ("Enhanced for loop output:");  for(Object object:collection) {System.out.println (object); }        //iterator TraversalSYSTEM.OUT.PRINTLN ("Iterator Traversal:"); Iterator<String> iterator=Collection.iterator ();  while(Iterator.hasnext ()) {System.out.println (Iterator.next ()); }        //determine if it containsBoolean boolean1=collection.contains ("AAA");        System.out.println (BOOLEAN1); //determines whether the emptyBoolean boolean2=Collection.isempty ();    System.out.println (BOOLEAN2); }}

Iterator Interface: All collection classes that implement the collection interface have a Iterator () method to return an object that implements the Iterator interface: Iterator it = Coll.iterator ();

How the iterator iterator works: The iterator is a specialized iterative output interface. The so-called iterative output is to judge the elements, determine whether there is content, if there is content to remove the content. The iterator object is called an iterator to facilitate the traversal of elements within the collection.

Any set that can be iterated with Iterator can be easily traversed using the enhanced for loop in the JDK5.0.

List interface:

The collection interface in Java--list, set, Map

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.