-A collection is an aggregate(or composition) in which more than one of each type is allowed.
-The number required is not known in advance.
e.g. Portfolio --> collection of Investments
-We have already seen HashMap from SingletonPerState
Collection Constructors
-Best way: create an empty collection object in the constructor.
-Also, provide a public add() method for loading objects into the collection. This method generally returns a boolean: false if it fails, true otherwise.
Collection Objects - 3 Types
Lists - objects in order, duplicates are OK
Sets - order does not matter, duplicates are not allowed.
Maps - objects are associated with keys.
See code for Portfolio
Iterators and Iterable
-Iterator and Iterable are interfaces.
-A class implementing an interface must implement all the methods specified in the interface exactly. This means the same return type, same name (case-sensitive), same number, order, and types of paramters.
--> MyClass implements MyInterface
public class MyClass implements MyInterface
{
.
.
.
}
The Iterator interface allows a method to take some action on each element in a collection. It contains a method that returns the collection's Iterator --> repackaging the collection for iterator operations.
See Rest of Portfolio code including getValue() method.
No comments:
Post a Comment