Collection commonly used functions
Collection of all single parent interface is set, thus defining a single common set of methods (and List Set) in Collection, these methods can be used to operate all separate collection. Methods as below:
- public boolean add (E e): the given object to the current collection.
- public void clear (): Empty the collection of all the elements.
- public boolean remove (E e): Delete the given object in the current collection.
- public boolean contains (E e): determining whether the current collection contains the given object.
- public boolean isEmpty (): this determines whether the set is empty.
- public int size (): returns the number of elements in a set.
- public Object [] toArray (): the set of elements stored in the array.
Code Example
Package demo01; Import of java.util.ArrayList; Import java.util.Collection; / * java.util.Collection interfaces topmost set of all single interface, defined inside a single set of common to all methods of any single set may be used collection method of interface methods in common: public Boolean the Add (E E): the given object to the current collection. public void clear (): Empty the collection of all the elements. public boolean remove (E e): Delete the given object in the current collection. public boolean contains (E e): determining whether the current collection contains the given object. public boolean isEmpty (): this determines whether the set is empty. public int size (): returns the number of elements in a set. public Object [] toArray (): the set of elements stored in the array. * / Public class Demo01Collection { public static void main (String [] args) { // create a set of objects, use polymorphic // Collection <String> = new new Coll the ArrayList <> (); Collection <String> = Coll new new the ArrayList <> (); the System. Out.println (Coll); // [] / * public Boolean the Add (E E): the given object to the current collection. The return value is a boolean value, generally returns true, so you can not received * / boolean B1 = coll.add ( "John Doe" ); System.out.println ( "B1:" + B1); // B1: to true System.out.println (Coll); // [Zhang] coll.add ( "John Doe" ); "John Doe" ); coll.add ( "Zhao six" ); coll.add ( "Tianqi" ); System.out.println (Coll); // [John Doe, John Doe Zhao six, pseudo-ginseng] / * public Boolean remove (E E): delete the given object in the current collection. The return value is a boolean value, the presence of elements in the set, removing elements, returns true collection element does not exist, the delete fails, returns to false * / boolean B2 = coll.remove ( "Zhao six" ); System.out.println ( " B2: "+ B2); // B2: to true Boolean B3 = coll.remove (" ZHAO Si " ); System.out.println ( " B3: "+ B3); System.out.println (Coll);// [John Doe, John Doe, pseudo-ginseng] / * public Boolean the contains (E E): this is determined whether the collection contains the given object. Returns true comprising not include the return to false * / Boolean B4 = coll.contains ( "John Doe" ); System.out.println ( "B4:" + B4); // B4: true Boolean B5 = coll.contains ( "Zhao four " ); System.out.println ( " B5: "+ B5); // B5: to false // public Boolean isEmpty (): this determines whether the set is empty. Returns a null set true, not empty return to false Boolean B6 = coll.isEmpty (); System.out.println ( "B6:" + B6); //B6: to false //public int size (): returns the number of elements in a set. int size = coll.size (); System.out.println ( "size:" + size); // size:. 4 // public Object [] toArray (): the set of elements stored in the array. Object [] = ARR coll.toArray (); for ( int I = 0; I <arr.length; I ++ ) { of System.out.print (ARR [I]); // Zhangsanlisi Doe TianQi [] } the System.out .println ( "" ); // public void the Clear (): empty the collection of all the elements. But not delete a collection, there is also a collection of coll.clear (); System.out.println (Coll); // [] System.out.println(coll.isEmpty());//true } }