Using Google Guava's Objects.ToStringHelper

前端 未结 6 528
生来不讨喜
生来不讨喜 2021-01-30 10:36

I used ToStringBuilder.reflectionToString(class) in commons-lang, to implement toString() for simple DTOs. Now I\'m trying to use Google Guava instead

6条回答
  •  爱一瞬间的悲伤
    2021-01-30 11:02

    MoreObjects.toStringHelper is intended to help you write toString() methods with a consistent format easily, but it gives you control over what fields you include in toString() and should have performance comparable to writing it out manually. reflectionToString is shorter to type, but it doesn't give you explicit control over the included fields and, well, it uses reflection. I don't see it as a better alternative.

    As a side note, I think using toStringHelper looks a lot cleaner if you put one add call per line.

    Guava docs

提交回复
热议问题