Java Introduction to Functional Programming Reduction and Aggregation Using a Range to Produce a Menu

Using the housing dataset, let's produce a menu of all states represented in the data.

Completely stumped on this section of the Introduction to Functional Programming Java course.

public static List<String> getStateCodesFromRecords(List<HousingRecord> records) {
    // TODO: Open a stream on records
      // TODO: Map the stream to the state code
      // TODO: Filter out any records without a state
      // TODO: There are duplicate state codes in the records, make sure we have a unique representation
      // TODO: Sort them alphabetically
      // TODO: Collect them into a new list.

  public static void displayStateCodeMenuDeclaratively(List<String> stateCodes) {
    // TODO: Use a range to display a numbered list of the states, starting at 1.
    IntStream.range(1, stateCodes.size())
      .mapToObj(i -> String.format("%d. %s ", i, stateCodes.get(i)))

As far as I can tell this should all work, but I'm getting these errors




The code for the whole project is downloadable from the exercise, if anyone can spot what I'm missing out on I'd really appreciate it.

edit: Solved menuIsInclusive by replacing

IntStream.range(1, stateCodes.size())
      .mapToObj(i -> String.format("%d. %s ", i, stateCodes.get(i)))


IntStream.rangeClosed(1, stateCodes.size())
      .mapToObj(i -> String.format("%d. %s ", i, stateCodes.get(i - 1)))

edit2: Solved stateCodesIgnoreBlanks and stateCodesAreSorted by adding a check for an empty string in addition to checking for null:

.filter(String -> String != null && !String.equals(""))

1 Answer

See edits below question.