Java Collection -001 Collection

Source: Internet
Author: User

Have not come to do a good job of the Java collection of commonly used classes, because the interview asked the collection of more in-depth not how to answer well, now come good likes and dislikes to fill.

Java collection classes are primarily derived from 2 interfaces: Collection, Map

The following figure is a set of collection tree, mainly derived from set, List, Queue three major categories



The following figure is a map-derived collection system tree



in this section we only discuss the methods of the collection interface

Add (Object O): This method is used to add an element

AddAll (Collection C): This method adds all the elements in the set C to the specified set

Clear (): clearly set all elements inside the collection

Contains (Object O): Returns whether the collection contains the specified element o

Containsall (Collection C): Returns whether the collection contains all the elements within the specified set C

IsEmpty (): Returns whether the collection is an empty collection

Iterator (): Returns a Iterator object user traversal collection

Remove (Object o): Deletes the specified element o

RemoveAll (collectoin c): Delete all the elements contained within the set C

Size (): Returns the number of elements in a collection

ToArray (): Converting a collection to an array

These methods are commonly used in our approach.


Traversal collection collection mainly useful iterator, for these 2 kinds of

Collection C = new ArrayList ();

C.add ("1");

C.add ("2");


Using iterator to traverse the collection C

Iterator it = C.iterator ();

while (It.hasnext ()) {

System.out.println (It.next ());

}


Using foreach to traverse the collection C

for (Object o:c) {

System.out.println (o);

}


Collection interface is introduced here will be a detailed introduction to set, list, map several commonly used interfaces.


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.