Collections Methods Flashcards
Java Collections Methods
coll.size()
returns an int that gives the number of objects in the collection.
coll.isEmpty()
returns a boolean value which is true if the size of the collection is 0.
coll.clear()
removes all objects from the collection.
coll.add(tobject)
adds tobject to the collection. The parameter must be of type T; if not, a syntax error occurs at compile time. The add() method returns a boolean value which tells you whether the operation actually modified the collection. For example, adding an object to a Set has no effect if that object was already in the set.
coll.contains(object)
returns a boolean value that is true if object is in the collection. Note that object is not required to be of type T, since it makes sense to check whether object is in the collection, no matter what type object has.
coll.remove(object)
removes object from the collection, if it occurs in the collection, and returns a boolean value that tells you whether the object was found. Again, object is not required to be of type T.
coll.containsAll(coll2)
returns a boolean value that is true if every object in coll2 is also in coll.
coll.addAll(coll2)
adds all the objects in coll2 to coll. The parameter, coll2, can be any collection of type Collection. However, it can also be more general. For example, if T is a class and S is a sub-class of T, then coll2 can be of type Collection.
coll.removeAll(coll2)
removes every object from coll that also occurs in the collection coll2. coll2 can be any collection.
coll.retainAll(coll2)
removes every object from coll that does not occur in the collection coll2. It “retains” only the objects that do occur in coll2. coll2 can be any collection.
coll.toArray()
returns an array of type Object[] that contains all the items in the collection. Note that the return type is Object[], not T[]! However, there is another version of this method that takes an array of type T[] as a parameter: the method coll.toArray(tarray) returns an array of type T[] containing all the items in the collection. If the array parameter tarray is large enough to hold the entire collection, then the items are stored in tarray and tarray is also the return value of the collection. If tarray is not large enough, then a new array is created to hold the items; in that case tarray serves only to specify the type of the array. For example, coll.toArray(new String[0]) can be used if coll is a collection of Strings and will return a new array of type String[].
coll.iterator()
f coll is a collection, then coll.iterator() returns an iterator that can be used to traverse the collection. Iterators are defined by a parameterized interface named Iterator. If coll implements the interface Collection for some specific type T, then coll.iterator() returns an iterator of type Iterator, with the same type T as its type parameter.
What are the three methods that are defined by the interface Iterator?
iter. next()
iter. hasNext()
iter. remove()
iter.next()
returns the next item, and advances the iterator. The return value is of type T.
iter.hasNext()
returns a boolean value telling you whether there are more items to be processed. In general, you should test this before calling iter.next().