Java Collection Knowledge Summary

Source: Internet
Author: User

One, the Java collection mainly has the collection and the map collection two interfaces, in Java the collection is to derive by these two interfaces.

The collection interface and the main implementation classes are as follows:

Collection Main API:

Boolean Add (Object obj);

Boolean AddAll (Collection c); Adds the elements of the set C to the specified collection.

void clear (); Clears all elements of the collection, the set length becomes 0

Boolean contains (Object O), whether the collection contains the specified element

Boolean containsall (Collection c); Does the collection contain all the elements in the set C

Boolean isEmpty (); Whether the collection is empty

Iterator Iterator (); Iterate through the elements in the collection

Boolean remove (Object o); Removes the specified element from the collection

Boolean retainall (Collection c); Removes elements from the collection that are not contained in collection C, which is equivalent to changing the collection that invokes the method into the intersection of the collection and the set C

int size (); Returns the number of collection elements

Object[] ToArray (); Convert a collection to an array

code example:

 Public classCollectiondemo { Public Static voidMain (string[] args) {//Create ArrayList CollectionCollection List=NewArrayList (); //adding elementsList.add (1); List.add ("Java"); List.add ("C + +"); System.out.println (list.tostring ()+ "Number of elements:" +list.size ()); //Delete ElementList.remove (1); System.out.println (list.tostring ()+ "Number of elements:" +list.size ()); //whether to include elementsSystem.out.println (List.contains ("Java")); //Create HashSet CollectionCollection Set=NewHashSet (); Set.add ("C #"); Set.add (C); Set.add ("Java"); //Determines whether the collection containsSystem.out.println (List.containsall (set)); //gets the intersection of two setsSet.retainall (list);    System.out.println (Set.tostring ()); }}

Java Collection Knowledge Summary

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.