Collection interface and traversal methods for Java collections

Source: Internet
Author: User

These are the several I follow the online tutorial to practice.

Url:

Http://www.cnblogs.com/jbelial/archive/2013/03/27/2981395.html

The feeling of playing code busty good.

Note When traversing the collection class, the Foreach loop provided by JDK 1.5 is more convenient instead of accessing the collection elements:

Note: When using iterator to access the Collection collection element, the elements inside the Collection collection cannot be changed (exceptions cannot be manipulated through the collection object's methods). Elements can only be manipulated by means of iterator.

The extraction method is defined inside the collection so that the extraction method can directly access the elements of the collection content. The removal method is defined as an inner class. The data structure of each container is different, so the details of the action are not the same, but there are common content: judging and removing

ImportJava.util.*; Public classCollectiontext { Public Static voidMain (string[] args) {Collection e=NewArrayList (); System.out.println ("Set is empty?:" +e.isempty ());    Collectionaddmethod (e); System.out.println ("Set is empty?:" +e.isempty ()); Iterator it=E.iterator ();  while(It.hasnext ()) {String ei=(String) it.next (); System.out.println ("Iterator Value:" +ei); }     for(Object obj:e) {String ei=(String) obj; System.out.println ("For value:" +ei); } System.out.println ("=============================");    Collectionremovemethod (e); System.out.println ("=============================");    Collectionretainallmethod (e); System.out.println ("=============================");  Collectioncontainsmethod (e); }   Public Static voidCollectionaddmethod (Collection e) {E.add ("I am Add 1"); E.add ("I am Add 2"); E.add ("I am Add 3"); E.add ("I am Add 4"); System.out.println ("After add element:");  System.out.println (e); }   Public Static voidCollectionremovemethod (Collection e) {e.remove ("I am Add 3"); System.out.println ("After remove element:");  System.out.println (e); }   Public Static voidCollectionretainallmethod (Collection e) {Collection e1=NewArrayList (); E1.add ("I am Add 4"); System.out.println ("Before Retainall,e is:" +e);    E.retainall (E1); System.out.println ("After Retainall, E-is:" +e); }   Public Static voidCollectioncontainsmethod (Collection e) {System.out.println ("If e has I am add 4?:" + e.contains ("I am add 4")); System.out.println ("If e has I am add 1?:" + e.contains ("I am add 1")); System.out.println ("If e has I am Add": "+ e.contains (" I am add 45 "))); }}

Collection interface and traversal methods for Java collections

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.