Collections Collection of Java

Source: Internet
Author: User
Tags addall int size min

The following table (table I) summarizes all the things you can do with a set (you can do the same thing with set and list, although the list also provides some extra functionality). The map is not inherited from collection, so it should be treated separately. The

Boolean Add (Object) guarantees that the collection contains an argument. If it does not add an argument, False (False)
Boolean addall (Collection) * Adds all the elements within the variable. Returns True (true)
void Clear () * Delete all elements within the collection if no element is added
Boolean contains (Object) if the collection contains an argument, returns the true
Boolean Containsall ( Collection) If the collection contains all the elements within the argument, return the "true"
Boolean isempty () if there is no element in the collection, return the "true"
Iterator iterator () returns a repeating device to use it to traverse the elements of the collection
Boolean Remove (Object) If the argument is in the collection, deletes an instance of that element. If it has been deleted, return the True
Boolean RemoveAll (Collection) * To remove all elements from the variable. If any deletions have been made, return the true
Boolean retainall (Collection) * Only the elements contained in an argument (a theoretical "intersection"). If any changes have been made, return the "true"
Size () returns the number of elements in the collection
Object[] ToArray () returns an array containing all the elements in the collection

Boolean Add (Object)

*ensures that the Collection contains the argument. Returns False if it doesn ' t add the argument.

Boolean AddAll (Collection)

*adds the elements in the argument. Returns true if any elements were added.

void Clear ()

*removes the elements in the Collection.

Boolean contains (Object)

True If the Collection contains the argument.

Boolean Containsall (Collection)

True If the Collection contains all of the elements in the argument.

Boolean IsEmpty ()

True if the Collection has no elements.

Iterator iterator ()

Returns an iterator this can use to move through the elements in the Collection.

Boolean Remove (Object)

*if The argument is in the Collection, one instance of this element is removed. Returns true if a removal occurred.

Boolean RemoveAll (Collection)

*removes all of the elements that are contained the argument. Returns true if any removals occurred.

Boolean Retainall (Collection)

*retains only elements this are contained in the argument (a "intersection" from set theory). Returns true if any changes occurred.

int size ()

Returns the number of elements in the Collection.

Object[] ToArray ()

Returns an array containing all the elements in the Collection.

Object[] ToArray (object[] a)

Returns an array containing all the elements in the Collection, whose type are that of the array a rather than PLA In Object (with the must cast the array to the right type).


*this is the "optional" method, which means it might the not to implemented by a particular Collection. If not, which is throws an unsupportedoperationexception. Exceptions is covered in Chapter 9.


Table I

* This is an "optional" method, and some collections may not implement it. If this is the case, the method will encounter a unsupportedoperatiionexception, that is, an "operation does not support" violation, as detailed in chapter 9th.

The following example shows you all the methods. Similarly, they are only valid for things inherited from the collection, and a ArrayList is used as a "less common denominator":

: Collection1.java//things can do with all collections package c08.newcollections;

Import java.util.*; public class Collection1 {//Fill with ' size ' elements, start//counting at ' start ': public static Collection F Ill (Collection c, int start, int size) {for (int i = start; I < start + size; i++) C.add (integer.tostring (i))
    ;
  return C; }//Default to a ' start ' of 0:public static Collection fill (Collection c, int size) {return fill (c, 0, size)
  ;
  }//Default to ten elements:public static Collection fill (Collection c) {return fill (c, 0, 10);
    }//Create & upcast to collection:public static Collection newcollection () {return Fill (new ArrayList ()); ArrayList is used to simplicity, but it ' s//only seen as a generic Collection//everywhere else in the P
  Rogram. }//Fill a Collection with a range of values:public static Collection newcollection (int start, int size) {RE Turn Fill (new ARraylist (), start, size); }//Moving through a List with the iterator:public static void print (Collection c) {for (iterator x = C.iterator ( );
      X.hasnext ();)
    System.out.print (X.next () + "");
  System.out.println ();
    public static void Main (string[] args) {Collection c = newcollection ();
    C.add ("ten");
    C.add ("eleven");
    print (c); 
    Make a array from the list:object[] array = C.toarray ();
    Make a String array from the list:string[] str = (string[]) C.toarray (new string[1)); Find Max and Min elements; This means//different things depending on the way//The comparable interface is IMPLEMENTED:SYSTEM.OUT.PRI
    Ntln ("Collections.max (c) =" + Collections.max (c));
    System.out.println ("collections.min (c) =" + Collections.min (c));
    Add a Collection to another Collection C.addall (Newcollection ());
    print (c); C.remove ("3");
    Removes the one print (c); C.remove ("3");
    Removes the second one print (c);
    Remove all components this are in the//Argument Collection:c.removeall (Newcollection ());
    print (c);
    C.addall (Newcollection ());
    print (c);
    is a element in this Collection System.out.println ("C.contains (\ 4\") = "+ C.contains (" 4 ")); is a Collection in this Collection System.out.println ("C.containsall (newcollection ()) =" + C.contain
    Sall (Newcollection ()));
    Collection C2 = newcollection (5, 3);
    Keep all the elements, are in both//C and C2 (a intersection of sets): C.retainall (C2);
    print (c);
    Throw away all "elements in C"//also appear in C2:c.removeall (C2);
    System.out.println ("c.isempty () =" + C.isempty ());
    c = newcollection ();
    print (c); C.clear ();
    Remove all elements System.out.println ("after C.clear ():");
  print (c); }
} ///:~

With the first method, we can populate any collection with test data. In the current case, only the int is converted to a string. The second method will be used frequently in the remainder of this chapter.
All two versions of Newcollection () have created ArrayList to contain different datasets and return them as collection objects. So it's obvious that there's no use for anything other than the collection interface.
The print () method is also used frequently in this section. Since it iterates through a set with a iterator, any set can produce such a repeating device, so it works for lists and sets, and also for collection generated by a map.
Main () shows all the methods in the collection in a simple way.
In a subsequent section, we will compare the different implementations of List,set and map, and indicate which scenario should be preferred (with the asterisk) in all situations. You will find that there are not some traditional classes such as Vector,stack and Hashtable. Because no matter what the case, the new collection has its own preferred class.

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.