Using collections before Java 9

Here is an example of how we would create our own collections prior to Java 9. This first class defines the structure for PlanetCollection. It has the following components:

  • A single instance variable
  • A one argument constructor
  • Mutator/setter method
  • Accessor/getter method
  • Method to print the object

Here is the code implementing the preceding listed constructor and methods:

    public class PlanetCollection     {      // Instance Variable      private String planetName;      // constructor      public PlanetCollection(String name)      {        setPlanetName(name);      }      // mutator      public void setPlanetName(String name)      {        this.planetName = name;      }      // accessor      public String getPlanetName()      {        return this.planetName;      }      public void print()      {        System.out ...

Get Java 9: Building Robust Modular Applications now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.