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