. 1 Package cn.learn.collection; 2 . 3 Import of java.util.ArrayList; . 4 Import java.util.Collection; . 5 . 6 / * . 7 in java.util.Collection in an Interface . 8 - the set of all single top level interface , which defines a common method for all single set . 9 - any single set may be used collection interface in process 10 . 11 common methods are: the Add, Clear, Remove, the contains, isEmpty, size, toString 12 is 13 is * / 14 public class CollectionInterface { 15 public static void main (String [] args) { 16 // create the collection object can be polymorphic, the interface implementation class pointed . 17 / * 18 is List <E> ordered set, data may coincide . 19 the Set <E> unordered, allowed coincidence data 20 is * / 21 is Collection <String> = Coll new new the ArrayList <> (); 22 is the System.out. the println (Coll); // [] toString method described override 23 is 24 // the Add, add elements to the collection and returns a Boolean 25 coll.add ( "ASD" ); 26 is // downward transition 27 ((the ArrayList <String>) Coll) .add ( "SS" ); 28 System.out.println (Coll); // [ASD, SS] 29 30 //remove, delete string element, it returns a Boolean 31 is coll.remove ( "SS" ); 32 System.out.println (Coll); // [ASD] 33 is // downcast call ArrayList rewriting remove, in accordance with remove the index element 34 is ((the ArrayList <string>) Coll) .remove (0 ); 35 System.out.println (Coll); // [] 36 37 [ // the contains, for determining whether a string comprising, a return Boolean 38 is System.out.println (coll.contains ( "")); // to false 39 40 // isEmpty, to determine whether the collection is empty, returns a Boolean value 41 is System.out.println (coll.isEmpty () ); //to true 42 is 43 is // size, used to determine set length, a return int 44 is System.out.println (coll.size ()); // 0 45 46 is // toArray, put into a set of arrays, can traverse, return an Object 47 coll.add ( "N1" ); 48 coll.add ( "N3" ); 49 coll.add ( "N2" ); 50 Object [] = ary coll.toArray (); 51 is System.out.println (ary [. 1]); // N3 52 is 53 is // Clear, empty set of all elements, without deleting the set 54 is coll.clear (); 55 56 is 57 is 58 } 59 60 }