Collections Methods Flashcards

Java Collections Methods

1
Q

coll.size()

A

returns an int that gives the number of objects in the collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

coll.isEmpty()

A

returns a boolean value which is true if the size of the collection is 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

coll.clear()

A

removes all objects from the collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

coll.add(tobject)

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

coll.contains(object)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

coll.remove(object)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

coll.containsAll(coll2)

A

returns a boolean value that is true if every object in coll2 is also in coll.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

coll.addAll(coll2)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

coll.removeAll(coll2)

A

removes every object from coll that also occurs in the collection coll2. coll2 can be any collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

coll.retainAll(coll2)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

coll.toArray()

A

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[].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

coll.iterator()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the three methods that are defined by the interface Iterator?

A

iter. next()
iter. hasNext()
iter. remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

iter.next()

A

returns the next item, and advances the iterator. The return value is of type T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

iter.hasNext()

A

returns a boolean value telling you whether there are more items to be processed. In general, you should test this before calling iter.next().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

iter.remove()

A

if you call this after calling iter.next(), it will remove the item that you just saw from the collection. Note that this method has no parameter. It removes the item that was most recently returned by iter.next(). This might produce an UnsupportedOperationException, if the collection does not support removal of items.

17
Q

Using iterators, we can write code for printing all the items in any collection.

A

Iterator iter; // Declare the iterator variable.
iter = coll.iterator(); // Get an iterator for the collection.
while ( iter.hasNext() ) {
String item = iter.next(); // Get the next item.
System.out.println(item);
}

18
Q

What is the syntax of the for-each loop in Java?

A
for (T x : coll) { // "for each object x, of type T, in coll"
     // process x 
}
x is the loop control variable. Each object in coll will be assigned to x in turn, and the body of the loop will be executed for each object. Since objects in coll are of type T, x is declared to be of type T.
19
Q

The == operator tests whether two objects are identical in the sense that they share the __ __ __ __ .

A

same location in memory.