I was refactoring an ugly piece of code, and I'm unsure if there is a better way of doing this, and whether I'm overdoing it with streams.

Basically I get a bunch of columns, which should be grouped by the name of the sheet they belong to, so that I can return a map of sheet names to a set of column names.

BTW, I can't change the return type (I know it should be a MultiMap).

public Map<String, Set<String>> getReferencedColumnNames(String columnName) {
    return _formulaSheetSchema.column(columnName).get().formula()
                    collectingAndThen(toSet(), set ->
  • \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$ Jun 29, 2019 at 8:18

1 Answer 1


I'd format this code slightly differently:

return _formulaSheetSchema.column(columnName).get()
    collectingAndThen(toSet(), set -> set.stream()

Also the downstream operation you use here is wasteful, because it collects the set and then restreams it. Instead use the mapping collector:

    mapping(ColumnRef::columnName, toSet()));

Sidenote: I personally prefer using 2-space indents across the board, but this formatting works with 4-space indents just as well...

Additionally the _-prefix for instance variables is something I dislike, but so long as you're consistent about it, that should be fine.

I also dislike that you're not checking whether the column actually exists, before accessing it's formula.

  • \$\begingroup\$ Thank you, that looks better, the collectingAndThen felt really clunky. The get() is safe in that context (there are sheet types without formulas, but the code is actually in a formula sheet). \$\endgroup\$
    – Landei
    Jun 22, 2019 at 21:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.